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 pa_resampler
* pa_resampler_new(
249 const pa_sample_spec
*a
,
250 const pa_channel_map
*am
,
251 const pa_sample_spec
*b
,
252 const pa_channel_map
*bm
,
253 pa_resample_method_t method
,
254 pa_resample_flags_t flags
) {
256 pa_resampler
*r
= NULL
;
261 pa_assert(pa_sample_spec_valid(a
));
262 pa_assert(pa_sample_spec_valid(b
));
263 pa_assert(method
>= 0);
264 pa_assert(method
< PA_RESAMPLER_MAX
);
266 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
268 r
= pa_xnew0(pa_resampler
, 1);
273 /* Fill sample specs */
277 /* set up the remap structure */
278 r
->remap
.i_ss
= &r
->i_ss
;
279 r
->remap
.o_ss
= &r
->o_ss
;
280 r
->remap
.format
= &r
->work_format
;
284 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
289 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
292 r
->i_fz
= pa_frame_size(a
);
293 r
->o_fz
= pa_frame_size(b
);
297 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
299 if ((method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
300 (method
== PA_RESAMPLER_FFMPEG
))
301 r
->work_format
= PA_SAMPLE_S16NE
;
302 else if (method
== PA_RESAMPLER_TRIVIAL
|| method
== PA_RESAMPLER_COPY
|| method
== PA_RESAMPLER_PEAKS
) {
304 if (r
->map_required
|| a
->format
!= b
->format
|| method
== PA_RESAMPLER_PEAKS
) {
306 if (a
->format
== PA_SAMPLE_S16NE
|| b
->format
== PA_SAMPLE_S16NE
)
307 r
->work_format
= PA_SAMPLE_S16NE
;
308 else if (a
->format
== PA_SAMPLE_S32NE
|| a
->format
== PA_SAMPLE_S32RE
||
309 a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
||
310 a
->format
== PA_SAMPLE_S24NE
|| a
->format
== PA_SAMPLE_S24RE
||
311 a
->format
== PA_SAMPLE_S24_32NE
|| a
->format
== PA_SAMPLE_S24_32RE
||
312 b
->format
== PA_SAMPLE_S32NE
|| b
->format
== PA_SAMPLE_S32RE
||
313 b
->format
== PA_SAMPLE_FLOAT32NE
|| b
->format
== PA_SAMPLE_FLOAT32RE
||
314 b
->format
== PA_SAMPLE_S24NE
|| b
->format
== PA_SAMPLE_S24RE
||
315 b
->format
== PA_SAMPLE_S24_32NE
|| b
->format
== PA_SAMPLE_S24_32RE
)
316 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
318 r
->work_format
= PA_SAMPLE_S16NE
;
321 r
->work_format
= a
->format
;
324 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
326 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
328 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
330 if (r
->i_ss
.format
!= r
->work_format
) {
331 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
332 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
335 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
336 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
341 if (r
->o_ss
.format
!= r
->work_format
) {
342 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
343 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
346 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
347 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
352 /* initialize implementation */
353 if (init_table
[method
](r
) < 0)
364 void pa_resampler_free(pa_resampler
*r
) {
370 if (r
->to_work_format_buf
.memblock
)
371 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
372 if (r
->remap_buf
.memblock
)
373 pa_memblock_unref(r
->remap_buf
.memblock
);
374 if (r
->resample_buf
.memblock
)
375 pa_memblock_unref(r
->resample_buf
.memblock
);
376 if (r
->from_work_format_buf
.memblock
)
377 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
379 pa_xfree(r
->impl_data
);
383 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
387 if (r
->i_ss
.rate
== rate
)
392 r
->impl_update_rates(r
);
395 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
399 if (r
->o_ss
.rate
== rate
)
404 r
->impl_update_rates(r
);
407 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
410 /* Let's round up here to make it more likely that the caller will get at
411 * least out_length amount of data from pa_resampler_run().
413 * We don't take the leftover into account here. If we did, then it might
414 * be in theory possible that this function would return 0 and
415 * pa_resampler_run() would also return 0. That could lead to infinite
416 * loops. When the leftover is ignored here, such loops would eventually
417 * terminate, because the leftover would grow each round, finally
418 * surpassing the minimum input threshold of the resampler. */
419 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
;
422 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
427 /* Let's round up here to ensure that the caller will always allocate big
428 * enough output buffer. */
430 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
432 if (r
->remap_buf_contains_leftover_data
)
433 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
435 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
438 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
439 size_t block_size_max
;
440 pa_sample_spec max_ss
;
446 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
448 /* We deduce the "largest" sample spec we're using during the
450 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
452 /* We silently assume that the format enum is ordered by size */
453 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
454 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
456 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
458 max_fs
= pa_frame_size(&max_ss
);
459 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
461 if (r
->remap_buf_contains_leftover_data
)
462 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
464 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
467 void pa_resampler_reset(pa_resampler
*r
) {
473 r
->remap_buf_contains_leftover_data
= false;
476 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
482 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
488 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
494 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
500 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
506 static const char * const resample_methods
[] = {
507 "src-sinc-best-quality",
508 "src-sinc-medium-quality",
510 "src-zero-order-hold",
541 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
543 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
546 return resample_methods
[m
];
549 int pa_resample_method_supported(pa_resample_method_t m
) {
551 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
554 #ifndef HAVE_LIBSAMPLERATE
555 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
560 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
562 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
569 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
570 pa_resample_method_t m
;
574 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
575 if (pa_streq(string
, resample_methods
[m
]))
578 if (pa_streq(string
, "speex-fixed"))
579 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
581 if (pa_streq(string
, "speex-float"))
582 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
584 return PA_RESAMPLER_INVALID
;
587 static bool on_left(pa_channel_position_t p
) {
590 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
591 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
592 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
593 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
594 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
595 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
598 static bool on_right(pa_channel_position_t p
) {
601 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
602 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
603 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
604 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
605 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
606 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
609 static bool on_center(pa_channel_position_t p
) {
612 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
613 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
614 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
615 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
616 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
619 static bool on_lfe(pa_channel_position_t p
) {
621 p
== PA_CHANNEL_POSITION_LFE
;
624 static bool on_front(pa_channel_position_t p
) {
626 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
627 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
628 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
629 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
630 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
631 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
632 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
633 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
636 static bool on_rear(pa_channel_position_t p
) {
638 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
639 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
640 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
641 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
642 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
643 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
646 static bool on_side(pa_channel_position_t p
) {
648 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
649 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
650 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
660 static int front_rear_side(pa_channel_position_t p
) {
670 static void calc_map_table(pa_resampler
*r
) {
673 bool ic_connected
[PA_CHANNELS_MAX
];
681 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
)))))
686 n_oc
= r
->o_ss
.channels
;
687 n_ic
= r
->i_ss
.channels
;
689 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
690 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
692 memset(ic_connected
, 0, sizeof(ic_connected
));
693 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
695 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
698 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
699 m
->map_table_f
[oc
][oc
] = 1.0f
;
701 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
703 for (oc
= 0; oc
< n_oc
; oc
++) {
704 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
706 for (ic
= 0; ic
< n_ic
; ic
++) {
707 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
709 /* We shall not do any remixing. Hence, just check by name */
711 m
->map_table_f
[oc
][ic
] = 1.0f
;
716 /* OK, we shall do the full monty: upmixing and downmixing. Our
717 * algorithm is relatively simple, does not do spacialization, delay
718 * elements or apply lowpass filters for LFE. Patches are always
719 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
720 * probably wouldn't make any sense anyway.)
722 * This code is not idempotent: downmixing an upmixed stereo stream is
723 * not identical to the original. The volume will not match, and the
724 * two channels will be a linear combination of both.
726 * This is loosely based on random suggestions found on the Internet,
728 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
731 * The algorithm works basically like this:
733 * 1) Connect all channels with matching names.
736 * S:Mono: Copy into all D:channels
737 * D:Mono: Avg all S:channels
739 * 3) Mix D:Left, D:Right:
740 * D:Left: If not connected, avg all S:Left
741 * D:Right: If not connected, avg all S:Right
744 * If not connected, avg all S:Center
745 * If still not connected, avg all S:Left, S:Right
748 * If not connected, avg all S:*
750 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
751 * connected, mix into all D:left and all D:right channels. Gain is
754 * 7) Make sure S:Center, S:LFE is used:
756 * S:Center, S:LFE: If not connected, mix into all D:left, all
757 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
758 * for LFE. C-front is only mixed into L-front/R-front if available,
759 * otherwise into all L/R channels. Similarly for C-rear.
761 * 8) Normalize each row in the matrix such that the sum for each row is
762 * not larger than 1.0 in order to avoid clipping.
764 * S: and D: shall relate to the source resp. destination channels.
766 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
767 * rear if needed. For 4: we try to find some suitable C source for C,
768 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
769 * channels. For 6: the rear channels should not be dropped entirely,
770 * however have only minimal impact. For 7: movies usually encode
771 * speech on the center channel. Thus we have to make sure this channel
772 * is distributed to L and R if not available in the output. Also, LFE
773 * is used to achieve a greater dynamic range, and thus we should try
774 * to do our best to pass it to L+R.
781 ic_unconnected_left
= 0,
782 ic_unconnected_right
= 0,
783 ic_unconnected_center
= 0,
784 ic_unconnected_lfe
= 0;
785 bool ic_unconnected_center_mixed_in
= 0;
789 for (ic
= 0; ic
< n_ic
; ic
++) {
790 if (on_left(r
->i_cm
.map
[ic
]))
792 if (on_right(r
->i_cm
.map
[ic
]))
794 if (on_center(r
->i_cm
.map
[ic
]))
798 for (oc
= 0; oc
< n_oc
; oc
++) {
799 bool oc_connected
= false;
800 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
802 for (ic
= 0; ic
< n_ic
; ic
++) {
803 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
805 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
806 m
->map_table_f
[oc
][ic
] = 1.0f
;
809 ic_connected
[ic
] = true;
811 else if (b
== PA_CHANNEL_POSITION_MONO
) {
812 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
815 ic_connected
[ic
] = true;
820 /* Try to find matching input ports for this output port */
824 /* We are not connected and on the left side, let's
825 * average all left side input channels. */
828 for (ic
= 0; ic
< n_ic
; ic
++)
829 if (on_left(r
->i_cm
.map
[ic
])) {
830 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
831 ic_connected
[ic
] = true;
834 /* We ignore the case where there is no left input channel.
835 * Something is really wrong in this case anyway. */
837 } else if (on_right(b
)) {
839 /* We are not connected and on the right side, let's
840 * average all right side input channels. */
843 for (ic
= 0; ic
< n_ic
; ic
++)
844 if (on_right(r
->i_cm
.map
[ic
])) {
845 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
846 ic_connected
[ic
] = true;
849 /* We ignore the case where there is no right input
850 * channel. Something is really wrong in this case anyway.
853 } else if (on_center(b
)) {
857 /* We are not connected and at the center. Let's average
858 * all center input channels. */
860 for (ic
= 0; ic
< n_ic
; ic
++)
861 if (on_center(r
->i_cm
.map
[ic
])) {
862 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
863 ic_connected
[ic
] = true;
866 } else if (ic_left
+ ic_right
> 0) {
868 /* Hmm, no center channel around, let's synthesize it
869 * by mixing L and R.*/
871 for (ic
= 0; ic
< n_ic
; ic
++)
872 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
873 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
874 ic_connected
[ic
] = true;
878 /* We ignore the case where there is not even a left or
879 * right input channel. Something is really wrong in this
882 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
884 /* We are not connected and an LFE. Let's average all
885 * channels for LFE. */
887 for (ic
= 0; ic
< n_ic
; ic
++)
888 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
890 /* Please note that a channel connected to LFE doesn't
891 * really count as connected. */
896 for (ic
= 0; ic
< n_ic
; ic
++) {
897 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
899 if (ic_connected
[ic
])
903 ic_unconnected_left
++;
904 else if (on_right(a
))
905 ic_unconnected_right
++;
906 else if (on_center(a
))
907 ic_unconnected_center
++;
909 ic_unconnected_lfe
++;
912 for (ic
= 0; ic
< n_ic
; ic
++) {
913 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
915 if (ic_connected
[ic
])
918 for (oc
= 0; oc
< n_oc
; oc
++) {
919 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
921 if (on_left(a
) && on_left(b
))
922 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
924 else if (on_right(a
) && on_right(b
))
925 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
927 else if (on_center(a
) && on_center(b
)) {
928 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
929 ic_unconnected_center_mixed_in
= true;
931 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
932 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
936 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
937 unsigned ncenter
[PA_CHANNELS_MAX
];
938 bool found_frs
[PA_CHANNELS_MAX
];
940 memset(ncenter
, 0, sizeof(ncenter
));
941 memset(found_frs
, 0, sizeof(found_frs
));
943 /* Hmm, as it appears there was no center channel we
944 could mix our center channel in. In this case, mix it into
945 left and right. Using .5 as the factor. */
947 for (ic
= 0; ic
< n_ic
; ic
++) {
949 if (ic_connected
[ic
])
952 if (!on_center(r
->i_cm
.map
[ic
]))
955 for (oc
= 0; oc
< n_oc
; oc
++) {
957 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
960 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
961 found_frs
[ic
] = true;
966 for (oc
= 0; oc
< n_oc
; oc
++) {
968 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
971 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
976 for (oc
= 0; oc
< n_oc
; oc
++) {
978 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
981 if (ncenter
[oc
] <= 0)
984 for (ic
= 0; ic
< n_ic
; ic
++) {
986 if (!on_center(r
->i_cm
.map
[ic
]))
989 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
990 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
996 for (oc
= 0; oc
< n_oc
; oc
++) {
998 for (ic
= 0; ic
< n_ic
; ic
++)
999 sum
+= m
->map_table_f
[oc
][ic
];
1002 for (ic
= 0; ic
< n_ic
; ic
++)
1003 m
->map_table_f
[oc
][ic
] /= sum
;
1006 /* make an 16:16 int version of the matrix */
1007 for (oc
= 0; oc
< n_oc
; oc
++)
1008 for (ic
= 0; ic
< n_ic
; ic
++)
1009 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1011 s
= pa_strbuf_new();
1013 pa_strbuf_printf(s
, " ");
1014 for (ic
= 0; ic
< n_ic
; ic
++)
1015 pa_strbuf_printf(s
, " I%02u ", ic
);
1016 pa_strbuf_puts(s
, "\n +");
1018 for (ic
= 0; ic
< n_ic
; ic
++)
1019 pa_strbuf_printf(s
, "------");
1020 pa_strbuf_puts(s
, "\n");
1022 for (oc
= 0; oc
< n_oc
; oc
++) {
1023 pa_strbuf_printf(s
, "O%02u |", oc
);
1025 for (ic
= 0; ic
< n_ic
; ic
++)
1026 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1028 pa_strbuf_puts(s
, "\n");
1031 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1034 /* initialize the remapping function */
1038 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1044 pa_assert(input
->memblock
);
1046 /* Convert the incoming sample into the work sample format and place them
1047 * in to_work_format_buf. */
1049 if (!r
->to_work_format_func
|| !input
->length
)
1052 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1054 r
->to_work_format_buf
.index
= 0;
1055 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1057 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1058 if (r
->to_work_format_buf
.memblock
)
1059 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1061 r
->to_work_format_buf_samples
= n_samples
;
1062 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1065 src
= pa_memblock_acquire_chunk(input
);
1066 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1068 r
->to_work_format_func(n_samples
, src
, dst
);
1070 pa_memblock_release(input
->memblock
);
1071 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1073 return &r
->to_work_format_buf
;
1076 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1077 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1079 size_t leftover_length
= 0;
1084 pa_assert(input
->memblock
);
1086 /* Remap channels and place the result in remap_buf. There may be leftover
1087 * data in the beginning of remap_buf. The leftover data is already
1088 * remapped, so it's not part of the input, it's part of the output. */
1090 have_leftover
= r
->remap_buf_contains_leftover_data
;
1091 r
->remap_buf_contains_leftover_data
= false;
1093 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1095 else if (input
->length
<= 0)
1096 return &r
->remap_buf
;
1098 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1099 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1101 if (have_leftover
) {
1102 leftover_length
= r
->remap_buf
.length
;
1103 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1106 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1107 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1109 if (have_leftover
) {
1110 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1111 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1113 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1114 dst
= pa_memblock_acquire(new_block
);
1115 memcpy(dst
, src
, leftover_length
);
1116 pa_memblock_release(r
->remap_buf
.memblock
);
1117 pa_memblock_release(new_block
);
1119 pa_memblock_unref(r
->remap_buf
.memblock
);
1120 r
->remap_buf
.memblock
= new_block
;
1121 r
->remap_buf_size
= r
->remap_buf
.length
;
1125 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1126 if (r
->remap_buf
.memblock
)
1127 pa_memblock_unref(r
->remap_buf
.memblock
);
1129 r
->remap_buf_size
= r
->remap_buf
.length
;
1130 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1134 src
= pa_memblock_acquire_chunk(input
);
1135 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1137 if (r
->map_required
) {
1138 pa_remap_t
*remap
= &r
->remap
;
1140 pa_assert(remap
->do_remap
);
1141 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1144 memcpy(dst
, src
, input
->length
);
1146 pa_memblock_release(input
->memblock
);
1147 pa_memblock_release(r
->remap_buf
.memblock
);
1149 return &r
->remap_buf
;
1152 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1153 unsigned in_n_frames
, in_n_samples
;
1154 unsigned out_n_frames
, out_n_samples
;
1159 /* Resample the data and place the result in resample_buf. */
1161 if (!r
->impl_resample
|| !input
->length
)
1164 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1165 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1167 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1168 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1170 r
->resample_buf
.index
= 0;
1171 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1173 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1174 if (r
->resample_buf
.memblock
)
1175 pa_memblock_unref(r
->resample_buf
.memblock
);
1177 r
->resample_buf_samples
= out_n_samples
;
1178 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1181 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1182 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1184 return &r
->resample_buf
;
1187 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1188 unsigned n_samples
, n_frames
;
1194 /* Convert the data into the correct sample type and place the result in
1195 * from_work_format_buf. */
1197 if (!r
->from_work_format_func
|| !input
->length
)
1200 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1201 n_frames
= n_samples
/ r
->o_ss
.channels
;
1203 r
->from_work_format_buf
.index
= 0;
1204 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1206 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1207 if (r
->from_work_format_buf
.memblock
)
1208 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1210 r
->from_work_format_buf_samples
= n_samples
;
1211 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1214 src
= pa_memblock_acquire_chunk(input
);
1215 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1216 r
->from_work_format_func(n_samples
, src
, dst
);
1217 pa_memblock_release(input
->memblock
);
1218 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1220 return &r
->from_work_format_buf
;
1223 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1229 pa_assert(in
->length
);
1230 pa_assert(in
->memblock
);
1231 pa_assert(in
->length
% r
->i_fz
== 0);
1233 buf
= (pa_memchunk
*) in
;
1234 buf
= convert_to_work_format(r
, buf
);
1235 buf
= remap_channels(r
, buf
);
1236 buf
= resample(r
, buf
);
1239 buf
= convert_from_work_format(r
, buf
);
1243 pa_memblock_ref(buf
->memblock
);
1245 pa_memchunk_reset(buf
);
1247 pa_memchunk_reset(out
);
1250 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1257 /* Store the leftover to remap_buf. */
1259 r
->remap_buf
.length
= len
;
1261 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1262 if (r
->remap_buf
.memblock
)
1263 pa_memblock_unref(r
->remap_buf
.memblock
);
1265 r
->remap_buf_size
= r
->remap_buf
.length
;
1266 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1269 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1270 memcpy(dst
, buf
, r
->remap_buf
.length
);
1271 pa_memblock_release(r
->remap_buf
.memblock
);
1273 r
->remap_buf_contains_leftover_data
= true;
1276 /*** libsamplerate based implementation ***/
1278 #ifdef HAVE_LIBSAMPLERATE
1279 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1281 struct src_data
*libsamplerate_data
;
1286 pa_assert(out_n_frames
);
1288 libsamplerate_data
= r
->impl_data
;
1289 memset(&data
, 0, sizeof(data
));
1291 data
.data_in
= pa_memblock_acquire_chunk(input
);
1292 data
.input_frames
= (long int) in_n_frames
;
1294 data
.data_out
= pa_memblock_acquire_chunk(output
);
1295 data
.output_frames
= (long int) *out_n_frames
;
1297 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1298 data
.end_of_input
= 0;
1300 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1302 if (data
.input_frames_used
< in_n_frames
) {
1303 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1304 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1306 save_leftover(r
, leftover_data
, leftover_length
);
1309 pa_memblock_release(input
->memblock
);
1310 pa_memblock_release(output
->memblock
);
1312 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1315 static void libsamplerate_update_rates(pa_resampler
*r
) {
1316 struct src_data
*libsamplerate_data
;
1319 libsamplerate_data
= r
->impl_data
;
1320 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1323 static void libsamplerate_reset(pa_resampler
*r
) {
1324 struct src_data
*libsamplerate_data
;
1327 libsamplerate_data
= r
->impl_data
;
1328 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1331 static void libsamplerate_free(pa_resampler
*r
) {
1332 struct src_data
*libsamplerate_data
;
1335 libsamplerate_data
= r
->impl_data
;
1336 if (libsamplerate_data
->state
)
1337 src_delete(libsamplerate_data
->state
);
1340 static int libsamplerate_init(pa_resampler
*r
) {
1342 struct src_data
*libsamplerate_data
;
1346 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1348 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1351 r
->impl_free
= libsamplerate_free
;
1352 r
->impl_update_rates
= libsamplerate_update_rates
;
1353 r
->impl_resample
= libsamplerate_resample
;
1354 r
->impl_reset
= libsamplerate_reset
;
1355 r
->impl_data
= libsamplerate_data
;
1362 /*** speex based implementation ***/
1364 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1366 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1367 struct speex_data
*speex_data
;
1372 pa_assert(out_n_frames
);
1374 speex_data
= r
->impl_data
;
1376 in
= pa_memblock_acquire_chunk(input
);
1377 out
= pa_memblock_acquire_chunk(output
);
1379 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1381 pa_memblock_release(input
->memblock
);
1382 pa_memblock_release(output
->memblock
);
1384 pa_assert(inf
== in_n_frames
);
1385 *out_n_frames
= outf
;
1388 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1390 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1391 struct speex_data
*speex_data
;
1396 pa_assert(out_n_frames
);
1398 speex_data
= r
->impl_data
;
1400 in
= pa_memblock_acquire_chunk(input
);
1401 out
= pa_memblock_acquire_chunk(output
);
1403 pa_assert_se(speex_resampler_process_interleaved_int(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1405 pa_memblock_release(input
->memblock
);
1406 pa_memblock_release(output
->memblock
);
1408 pa_assert(inf
== in_n_frames
);
1409 *out_n_frames
= outf
;
1412 static void speex_update_rates(pa_resampler
*r
) {
1413 struct speex_data
*speex_data
;
1416 speex_data
= r
->impl_data
;
1418 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1421 static void speex_reset(pa_resampler
*r
) {
1422 struct speex_data
*speex_data
;
1425 speex_data
= r
->impl_data
;
1427 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1430 static void speex_free(pa_resampler
*r
) {
1431 struct speex_data
*speex_data
;
1434 speex_data
= r
->impl_data
;
1435 if (!speex_data
->state
)
1438 speex_resampler_destroy(speex_data
->state
);
1441 static int speex_init(pa_resampler
*r
) {
1443 struct speex_data
*speex_data
;
1447 speex_data
= pa_xnew(struct speex_data
, 1);
1449 r
->impl_free
= speex_free
;
1450 r
->impl_update_rates
= speex_update_rates
;
1451 r
->impl_reset
= speex_reset
;
1452 r
->impl_data
= speex_data
;
1454 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1456 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1457 r
->impl_resample
= speex_resample_int
;
1460 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1462 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1463 r
->impl_resample
= speex_resample_float
;
1466 pa_log_info("Choosing speex quality setting %i.", q
);
1468 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1475 /* Trivial implementation */
1477 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1479 unsigned i_index
, o_index
;
1481 struct trivial_data
*trivial_data
;
1486 pa_assert(out_n_frames
);
1488 trivial_data
= r
->impl_data
;
1489 fz
= r
->w_sz
* r
->o_ss
.channels
;
1491 src
= pa_memblock_acquire_chunk(input
);
1492 dst
= pa_memblock_acquire_chunk(output
);
1494 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1495 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1496 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1498 if (i_index
>= in_n_frames
)
1501 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1503 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1506 pa_memblock_release(input
->memblock
);
1507 pa_memblock_release(output
->memblock
);
1509 *out_n_frames
= o_index
;
1511 trivial_data
->i_counter
+= in_n_frames
;
1513 /* Normalize counters */
1514 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1515 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1517 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1518 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1522 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1523 struct trivial_data
*trivial_data
;
1526 trivial_data
= r
->impl_data
;
1528 trivial_data
->i_counter
= 0;
1529 trivial_data
->o_counter
= 0;
1532 static int trivial_init(pa_resampler
*r
) {
1533 struct trivial_data
*trivial_data
;
1536 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1538 r
->impl_resample
= trivial_resample
;
1539 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1540 r
->impl_reset
= trivial_update_rates_or_reset
;
1541 r
->impl_data
= trivial_data
;
1546 /* Peak finder implementation */
1548 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1549 unsigned c
, o_index
= 0;
1550 unsigned i
, i_end
= 0;
1552 struct peaks_data
*peaks_data
;
1557 pa_assert(out_n_frames
);
1559 peaks_data
= r
->impl_data
;
1560 src
= pa_memblock_acquire_chunk(input
);
1561 dst
= pa_memblock_acquire_chunk(output
);
1563 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1564 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1566 while (i_end
< in_n_frames
) {
1567 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1568 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1570 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1572 /* 1ch float is treated separately, because that is the common case */
1573 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1574 float *s
= (float*) src
+ i
;
1575 float *d
= (float*) dst
+ o_index
;
1577 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1578 float n
= fabsf(*s
++);
1580 if (n
> peaks_data
->max_f
[0])
1581 peaks_data
->max_f
[0] = n
;
1585 *d
= peaks_data
->max_f
[0];
1586 peaks_data
->max_f
[0] = 0;
1587 o_index
++, peaks_data
->o_counter
++;
1589 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1590 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1591 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1593 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1594 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1595 int16_t n
= abs(*s
++);
1597 if (n
> peaks_data
->max_i
[c
])
1598 peaks_data
->max_i
[c
] = n
;
1602 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1603 *d
= peaks_data
->max_i
[c
];
1604 peaks_data
->max_i
[c
] = 0;
1606 o_index
++, peaks_data
->o_counter
++;
1609 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1610 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1612 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1613 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1614 float n
= fabsf(*s
++);
1616 if (n
> peaks_data
->max_f
[c
])
1617 peaks_data
->max_f
[c
] = n
;
1621 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1622 *d
= peaks_data
->max_f
[c
];
1623 peaks_data
->max_f
[c
] = 0;
1625 o_index
++, peaks_data
->o_counter
++;
1630 pa_memblock_release(input
->memblock
);
1631 pa_memblock_release(output
->memblock
);
1633 *out_n_frames
= o_index
;
1635 peaks_data
->i_counter
+= in_n_frames
;
1637 /* Normalize counters */
1638 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1639 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1641 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1642 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1646 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1647 struct peaks_data
*peaks_data
;
1650 peaks_data
= r
->impl_data
;
1652 peaks_data
->i_counter
= 0;
1653 peaks_data
->o_counter
= 0;
1656 static int peaks_init(pa_resampler
*r
) {
1657 struct peaks_data
*peaks_data
;
1659 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1660 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1662 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1664 r
->impl_resample
= peaks_resample
;
1665 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1666 r
->impl_reset
= peaks_update_rates_or_reset
;
1667 r
->impl_data
= peaks_data
;
1672 /*** ffmpeg based implementation ***/
1674 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1675 unsigned used_frames
= 0, c
;
1676 int previous_consumed_frames
= -1;
1677 struct ffmpeg_data
*ffmpeg_data
;
1682 pa_assert(out_n_frames
);
1684 ffmpeg_data
= r
->impl_data
;
1686 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1689 int16_t *p
, *t
, *k
, *q
, *s
;
1690 int consumed_frames
;
1692 /* Allocate a new block */
1693 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1694 p
= pa_memblock_acquire(b
);
1696 /* Now copy the input data, splitting up channels */
1697 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1699 for (u
= 0; u
< in_n_frames
; u
++) {
1701 t
+= r
->o_ss
.channels
;
1704 pa_memblock_release(input
->memblock
);
1706 /* Allocate buffer for the result */
1707 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1708 q
= pa_memblock_acquire(w
);
1711 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1714 (int) in_n_frames
, (int) *out_n_frames
,
1715 c
>= (unsigned) (r
->o_ss
.channels
-1));
1717 pa_memblock_release(b
);
1718 pa_memblock_unref(b
);
1720 pa_assert(consumed_frames
<= (int) in_n_frames
);
1721 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1722 previous_consumed_frames
= consumed_frames
;
1724 /* And place the results in the output buffer */
1725 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1726 for (u
= 0; u
< used_frames
; u
++) {
1729 s
+= r
->o_ss
.channels
;
1731 pa_memblock_release(output
->memblock
);
1732 pa_memblock_release(w
);
1733 pa_memblock_unref(w
);
1736 if (previous_consumed_frames
< (int) in_n_frames
) {
1737 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1738 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1740 save_leftover(r
, leftover_data
, leftover_length
);
1741 pa_memblock_release(input
->memblock
);
1744 *out_n_frames
= used_frames
;
1747 static void ffmpeg_free(pa_resampler
*r
) {
1749 struct ffmpeg_data
*ffmpeg_data
;
1753 ffmpeg_data
= r
->impl_data
;
1754 if (ffmpeg_data
->state
)
1755 av_resample_close(ffmpeg_data
->state
);
1757 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1758 if (ffmpeg_data
->buf
[c
].memblock
)
1759 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1762 static int ffmpeg_init(pa_resampler
*r
) {
1764 struct ffmpeg_data
*ffmpeg_data
;
1768 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1770 /* We could probably implement different quality levels by
1771 * adjusting the filter parameters here. However, ffmpeg
1772 * internally only uses these hardcoded values, so let's use them
1773 * here for now as well until ffmpeg makes this configurable. */
1775 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1778 r
->impl_free
= ffmpeg_free
;
1779 r
->impl_resample
= ffmpeg_resample
;
1780 r
->impl_data
= (void *) ffmpeg_data
;
1782 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1783 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1788 /*** copy (noop) implementation ***/
1790 static int copy_init(pa_resampler
*r
) {
1793 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);