2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
33 #include <speex/speex_resampler.h>
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/remap.h>
42 #include <pulsecore/core-util.h>
43 #include "ffmpeg/avcodec.h"
45 #include "resampler.h"
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
51 pa_resample_method_t method
;
52 pa_resample_flags_t flags
;
54 pa_sample_spec i_ss
, o_ss
;
55 pa_channel_map i_cm
, o_cm
;
56 size_t i_fz
, o_fz
, w_sz
;
59 pa_memchunk to_work_format_buf
;
60 pa_memchunk remap_buf
;
61 pa_memchunk resample_buf
;
62 pa_memchunk from_work_format_buf
;
63 unsigned to_work_format_buf_samples
;
64 size_t remap_buf_size
;
65 unsigned resample_buf_samples
;
66 unsigned from_work_format_buf_samples
;
67 bool remap_buf_contains_leftover_data
;
69 pa_sample_format_t work_format
;
71 pa_convert_func_t to_work_format_func
;
72 pa_convert_func_t from_work_format_func
;
77 void (*impl_free
)(pa_resampler
*r
);
78 void (*impl_update_rates
)(pa_resampler
*r
);
79 void (*impl_resample
)(pa_resampler
*r
, const pa_memchunk
*in
, unsigned in_samples
, pa_memchunk
*out
, unsigned *out_samples
);
80 void (*impl_reset
)(pa_resampler
*r
);
84 struct trivial_data
{ /* data specific to the trivial resampler */
89 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
93 float max_f
[PA_CHANNELS_MAX
];
94 int16_t max_i
[PA_CHANNELS_MAX
];
97 #ifdef HAVE_LIBSAMPLERATE
98 struct src_data
{ /* data specific to libsamplerate */
104 struct speex_data
{ /* data specific to speex */
105 SpeexResamplerState
* state
;
109 struct ffmpeg_data
{ /* data specific to ffmpeg */
110 struct AVResampleContext
*state
;
111 pa_memchunk buf
[PA_CHANNELS_MAX
];
114 static int copy_init(pa_resampler
*r
);
115 static int trivial_init(pa_resampler
*r
);
117 static int speex_init(pa_resampler
*r
);
119 static int ffmpeg_init(pa_resampler
*r
);
120 static int peaks_init(pa_resampler
*r
);
121 #ifdef HAVE_LIBSAMPLERATE
122 static int libsamplerate_init(pa_resampler
*r
);
125 static void calc_map_table(pa_resampler
*r
);
127 static int (* const init_table
[])(pa_resampler
*r
) = {
128 #ifdef HAVE_LIBSAMPLERATE
129 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
130 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
131 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
132 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
133 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
135 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
136 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
137 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
138 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
139 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
141 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
176 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
187 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
189 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
190 [PA_RESAMPLER_AUTO
] = NULL
,
191 [PA_RESAMPLER_COPY
] = copy_init
,
192 [PA_RESAMPLER_PEAKS
] = peaks_init
,
195 static pa_resample_method_t
pa_resampler_fix_method(
196 pa_resample_flags_t flags
,
197 pa_resample_method_t method
,
198 const uint32_t rate_a
,
199 const uint32_t rate_b
) {
201 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
202 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
203 pa_assert(method
>= 0);
204 pa_assert(method
< PA_RESAMPLER_MAX
);
206 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
207 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
208 method
= PA_RESAMPLER_COPY
;
211 if (!pa_resample_method_supported(method
)) {
212 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
213 method
= PA_RESAMPLER_AUTO
;
217 case PA_RESAMPLER_COPY
:
218 if (rate_a
!= rate_b
) {
219 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
222 /* Else fall through */
223 case PA_RESAMPLER_FFMPEG
:
224 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
225 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
226 method
= PA_RESAMPLER_AUTO
;
233 if (method
== PA_RESAMPLER_AUTO
) {
235 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
237 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
238 method
= PA_RESAMPLER_TRIVIAL
;
240 method
= PA_RESAMPLER_FFMPEG
;
247 static pa_sample_format_t
pa_resampler_choose_work_format(
248 pa_resample_method_t method
,
249 pa_sample_format_t a
,
250 pa_sample_format_t b
,
252 pa_sample_format_t work_format
;
254 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
255 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
256 pa_assert(method
>= 0);
257 pa_assert(method
< PA_RESAMPLER_MAX
);
259 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
260 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
263 /* This block is for resampling functions that only
264 * support the S16 sample format. */
265 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
266 case PA_RESAMPLER_FFMPEG
:
267 work_format
= PA_SAMPLE_S16NE
;
270 /* This block is for resampling functions that support
271 * any sample format. */
272 case PA_RESAMPLER_COPY
: /* fall through */
273 case PA_RESAMPLER_TRIVIAL
:
274 if (!map_required
&& a
== b
) {
278 /* Else fall trough */
279 case PA_RESAMPLER_PEAKS
:
280 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
281 work_format
= PA_SAMPLE_S16NE
;
282 else if (a
== PA_SAMPLE_S32NE
|| a
== PA_SAMPLE_S32RE
||
283 a
== PA_SAMPLE_FLOAT32NE
|| a
== PA_SAMPLE_FLOAT32RE
||
284 a
== PA_SAMPLE_S24NE
|| a
== PA_SAMPLE_S24RE
||
285 a
== PA_SAMPLE_S24_32NE
|| a
== PA_SAMPLE_S24_32RE
||
286 b
== PA_SAMPLE_S32NE
|| b
== PA_SAMPLE_S32RE
||
287 b
== PA_SAMPLE_FLOAT32NE
|| b
== PA_SAMPLE_FLOAT32RE
||
288 b
== PA_SAMPLE_S24NE
|| b
== PA_SAMPLE_S24RE
||
289 b
== PA_SAMPLE_S24_32NE
|| b
== PA_SAMPLE_S24_32RE
)
290 work_format
= PA_SAMPLE_FLOAT32NE
;
292 work_format
= PA_SAMPLE_S16NE
;
296 work_format
= PA_SAMPLE_FLOAT32NE
;
302 pa_resampler
* pa_resampler_new(
304 const pa_sample_spec
*a
,
305 const pa_channel_map
*am
,
306 const pa_sample_spec
*b
,
307 const pa_channel_map
*bm
,
308 pa_resample_method_t method
,
309 pa_resample_flags_t flags
) {
311 pa_resampler
*r
= NULL
;
316 pa_assert(pa_sample_spec_valid(a
));
317 pa_assert(pa_sample_spec_valid(b
));
318 pa_assert(method
>= 0);
319 pa_assert(method
< PA_RESAMPLER_MAX
);
321 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
323 r
= pa_xnew0(pa_resampler
, 1);
328 /* Fill sample specs */
332 /* set up the remap structure */
333 r
->remap
.i_ss
= &r
->i_ss
;
334 r
->remap
.o_ss
= &r
->o_ss
;
335 r
->remap
.format
= &r
->work_format
;
339 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
344 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
347 r
->i_fz
= pa_frame_size(a
);
348 r
->o_fz
= pa_frame_size(b
);
352 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
354 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
356 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
358 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
360 if (r
->i_ss
.format
!= r
->work_format
) {
361 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
362 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
365 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
366 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
371 if (r
->o_ss
.format
!= r
->work_format
) {
372 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
373 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
376 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
377 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
382 /* initialize implementation */
383 if (init_table
[method
](r
) < 0)
394 void pa_resampler_free(pa_resampler
*r
) {
400 if (r
->to_work_format_buf
.memblock
)
401 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
402 if (r
->remap_buf
.memblock
)
403 pa_memblock_unref(r
->remap_buf
.memblock
);
404 if (r
->resample_buf
.memblock
)
405 pa_memblock_unref(r
->resample_buf
.memblock
);
406 if (r
->from_work_format_buf
.memblock
)
407 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
409 pa_xfree(r
->impl_data
);
413 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
417 if (r
->i_ss
.rate
== rate
)
422 r
->impl_update_rates(r
);
425 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
429 if (r
->o_ss
.rate
== rate
)
434 r
->impl_update_rates(r
);
437 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
440 /* Let's round up here to make it more likely that the caller will get at
441 * least out_length amount of data from pa_resampler_run().
443 * We don't take the leftover into account here. If we did, then it might
444 * be in theory possible that this function would return 0 and
445 * pa_resampler_run() would also return 0. That could lead to infinite
446 * loops. When the leftover is ignored here, such loops would eventually
447 * terminate, because the leftover would grow each round, finally
448 * surpassing the minimum input threshold of the resampler. */
449 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
;
452 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
457 /* Let's round up here to ensure that the caller will always allocate big
458 * enough output buffer. */
460 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
462 if (r
->remap_buf_contains_leftover_data
)
463 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
465 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
468 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
469 size_t block_size_max
;
470 pa_sample_spec max_ss
;
476 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
478 /* We deduce the "largest" sample spec we're using during the
480 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
482 /* We silently assume that the format enum is ordered by size */
483 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
484 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
486 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
488 max_fs
= pa_frame_size(&max_ss
);
489 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
491 if (r
->remap_buf_contains_leftover_data
)
492 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
494 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
497 void pa_resampler_reset(pa_resampler
*r
) {
503 r
->remap_buf_contains_leftover_data
= false;
506 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
512 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
518 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
524 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
530 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
536 static const char * const resample_methods
[] = {
537 "src-sinc-best-quality",
538 "src-sinc-medium-quality",
540 "src-zero-order-hold",
571 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
573 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
576 return resample_methods
[m
];
579 int pa_resample_method_supported(pa_resample_method_t m
) {
581 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
584 #ifndef HAVE_LIBSAMPLERATE
585 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
590 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
592 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
599 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
600 pa_resample_method_t m
;
604 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
605 if (pa_streq(string
, resample_methods
[m
]))
608 if (pa_streq(string
, "speex-fixed"))
609 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
611 if (pa_streq(string
, "speex-float"))
612 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
614 return PA_RESAMPLER_INVALID
;
617 static bool on_left(pa_channel_position_t p
) {
620 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
621 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
622 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
623 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
624 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
625 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
628 static bool on_right(pa_channel_position_t p
) {
631 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
632 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
633 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
634 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
635 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
636 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
639 static bool on_center(pa_channel_position_t p
) {
642 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
643 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
644 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
645 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
646 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
649 static bool on_lfe(pa_channel_position_t p
) {
651 p
== PA_CHANNEL_POSITION_LFE
;
654 static bool on_front(pa_channel_position_t p
) {
656 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
657 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
658 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
659 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
660 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
661 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
662 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
663 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
666 static bool on_rear(pa_channel_position_t p
) {
668 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
669 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
670 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
671 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
672 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
673 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
676 static bool on_side(pa_channel_position_t p
) {
678 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
679 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
680 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
690 static int front_rear_side(pa_channel_position_t p
) {
700 static void calc_map_table(pa_resampler
*r
) {
703 bool ic_connected
[PA_CHANNELS_MAX
];
711 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
)))))
716 n_oc
= r
->o_ss
.channels
;
717 n_ic
= r
->i_ss
.channels
;
719 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
720 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
722 memset(ic_connected
, 0, sizeof(ic_connected
));
723 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
725 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
728 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
729 m
->map_table_f
[oc
][oc
] = 1.0f
;
731 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
733 for (oc
= 0; oc
< n_oc
; oc
++) {
734 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
736 for (ic
= 0; ic
< n_ic
; ic
++) {
737 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
739 /* We shall not do any remixing. Hence, just check by name */
741 m
->map_table_f
[oc
][ic
] = 1.0f
;
746 /* OK, we shall do the full monty: upmixing and downmixing. Our
747 * algorithm is relatively simple, does not do spacialization, delay
748 * elements or apply lowpass filters for LFE. Patches are always
749 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
750 * probably wouldn't make any sense anyway.)
752 * This code is not idempotent: downmixing an upmixed stereo stream is
753 * not identical to the original. The volume will not match, and the
754 * two channels will be a linear combination of both.
756 * This is loosely based on random suggestions found on the Internet,
758 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
761 * The algorithm works basically like this:
763 * 1) Connect all channels with matching names.
766 * S:Mono: Copy into all D:channels
767 * D:Mono: Avg all S:channels
769 * 3) Mix D:Left, D:Right:
770 * D:Left: If not connected, avg all S:Left
771 * D:Right: If not connected, avg all S:Right
774 * If not connected, avg all S:Center
775 * If still not connected, avg all S:Left, S:Right
778 * If not connected, avg all S:*
780 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
781 * connected, mix into all D:left and all D:right channels. Gain is
784 * 7) Make sure S:Center, S:LFE is used:
786 * S:Center, S:LFE: If not connected, mix into all D:left, all
787 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
788 * for LFE. C-front is only mixed into L-front/R-front if available,
789 * otherwise into all L/R channels. Similarly for C-rear.
791 * 8) Normalize each row in the matrix such that the sum for each row is
792 * not larger than 1.0 in order to avoid clipping.
794 * S: and D: shall relate to the source resp. destination channels.
796 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
797 * rear if needed. For 4: we try to find some suitable C source for C,
798 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
799 * channels. For 6: the rear channels should not be dropped entirely,
800 * however have only minimal impact. For 7: movies usually encode
801 * speech on the center channel. Thus we have to make sure this channel
802 * is distributed to L and R if not available in the output. Also, LFE
803 * is used to achieve a greater dynamic range, and thus we should try
804 * to do our best to pass it to L+R.
811 ic_unconnected_left
= 0,
812 ic_unconnected_right
= 0,
813 ic_unconnected_center
= 0,
814 ic_unconnected_lfe
= 0;
815 bool ic_unconnected_center_mixed_in
= 0;
819 for (ic
= 0; ic
< n_ic
; ic
++) {
820 if (on_left(r
->i_cm
.map
[ic
]))
822 if (on_right(r
->i_cm
.map
[ic
]))
824 if (on_center(r
->i_cm
.map
[ic
]))
828 for (oc
= 0; oc
< n_oc
; oc
++) {
829 bool oc_connected
= false;
830 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
832 for (ic
= 0; ic
< n_ic
; ic
++) {
833 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
835 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
836 m
->map_table_f
[oc
][ic
] = 1.0f
;
839 ic_connected
[ic
] = true;
841 else if (b
== PA_CHANNEL_POSITION_MONO
) {
842 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
845 ic_connected
[ic
] = true;
850 /* Try to find matching input ports for this output port */
854 /* We are not connected and on the left side, let's
855 * average all left side input channels. */
858 for (ic
= 0; ic
< n_ic
; ic
++)
859 if (on_left(r
->i_cm
.map
[ic
])) {
860 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
861 ic_connected
[ic
] = true;
864 /* We ignore the case where there is no left input channel.
865 * Something is really wrong in this case anyway. */
867 } else if (on_right(b
)) {
869 /* We are not connected and on the right side, let's
870 * average all right side input channels. */
873 for (ic
= 0; ic
< n_ic
; ic
++)
874 if (on_right(r
->i_cm
.map
[ic
])) {
875 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
876 ic_connected
[ic
] = true;
879 /* We ignore the case where there is no right input
880 * channel. Something is really wrong in this case anyway.
883 } else if (on_center(b
)) {
887 /* We are not connected and at the center. Let's average
888 * all center input channels. */
890 for (ic
= 0; ic
< n_ic
; ic
++)
891 if (on_center(r
->i_cm
.map
[ic
])) {
892 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
893 ic_connected
[ic
] = true;
896 } else if (ic_left
+ ic_right
> 0) {
898 /* Hmm, no center channel around, let's synthesize it
899 * by mixing L and R.*/
901 for (ic
= 0; ic
< n_ic
; ic
++)
902 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
903 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
904 ic_connected
[ic
] = true;
908 /* We ignore the case where there is not even a left or
909 * right input channel. Something is really wrong in this
912 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
914 /* We are not connected and an LFE. Let's average all
915 * channels for LFE. */
917 for (ic
= 0; ic
< n_ic
; ic
++)
918 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
920 /* Please note that a channel connected to LFE doesn't
921 * really count as connected. */
926 for (ic
= 0; ic
< n_ic
; ic
++) {
927 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
929 if (ic_connected
[ic
])
933 ic_unconnected_left
++;
934 else if (on_right(a
))
935 ic_unconnected_right
++;
936 else if (on_center(a
))
937 ic_unconnected_center
++;
939 ic_unconnected_lfe
++;
942 for (ic
= 0; ic
< n_ic
; ic
++) {
943 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
945 if (ic_connected
[ic
])
948 for (oc
= 0; oc
< n_oc
; oc
++) {
949 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
951 if (on_left(a
) && on_left(b
))
952 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
954 else if (on_right(a
) && on_right(b
))
955 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
957 else if (on_center(a
) && on_center(b
)) {
958 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
959 ic_unconnected_center_mixed_in
= true;
961 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
962 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
966 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
967 unsigned ncenter
[PA_CHANNELS_MAX
];
968 bool found_frs
[PA_CHANNELS_MAX
];
970 memset(ncenter
, 0, sizeof(ncenter
));
971 memset(found_frs
, 0, sizeof(found_frs
));
973 /* Hmm, as it appears there was no center channel we
974 could mix our center channel in. In this case, mix it into
975 left and right. Using .5 as the factor. */
977 for (ic
= 0; ic
< n_ic
; ic
++) {
979 if (ic_connected
[ic
])
982 if (!on_center(r
->i_cm
.map
[ic
]))
985 for (oc
= 0; oc
< n_oc
; oc
++) {
987 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
990 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
991 found_frs
[ic
] = true;
996 for (oc
= 0; oc
< n_oc
; oc
++) {
998 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1001 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1006 for (oc
= 0; oc
< n_oc
; oc
++) {
1008 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1011 if (ncenter
[oc
] <= 0)
1014 for (ic
= 0; ic
< n_ic
; ic
++) {
1016 if (!on_center(r
->i_cm
.map
[ic
]))
1019 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1020 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1026 for (oc
= 0; oc
< n_oc
; oc
++) {
1028 for (ic
= 0; ic
< n_ic
; ic
++)
1029 sum
+= m
->map_table_f
[oc
][ic
];
1032 for (ic
= 0; ic
< n_ic
; ic
++)
1033 m
->map_table_f
[oc
][ic
] /= sum
;
1036 /* make an 16:16 int version of the matrix */
1037 for (oc
= 0; oc
< n_oc
; oc
++)
1038 for (ic
= 0; ic
< n_ic
; ic
++)
1039 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1041 s
= pa_strbuf_new();
1043 pa_strbuf_printf(s
, " ");
1044 for (ic
= 0; ic
< n_ic
; ic
++)
1045 pa_strbuf_printf(s
, " I%02u ", ic
);
1046 pa_strbuf_puts(s
, "\n +");
1048 for (ic
= 0; ic
< n_ic
; ic
++)
1049 pa_strbuf_printf(s
, "------");
1050 pa_strbuf_puts(s
, "\n");
1052 for (oc
= 0; oc
< n_oc
; oc
++) {
1053 pa_strbuf_printf(s
, "O%02u |", oc
);
1055 for (ic
= 0; ic
< n_ic
; ic
++)
1056 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1058 pa_strbuf_puts(s
, "\n");
1061 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1064 /* initialize the remapping function */
1068 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1074 pa_assert(input
->memblock
);
1076 /* Convert the incoming sample into the work sample format and place them
1077 * in to_work_format_buf. */
1079 if (!r
->to_work_format_func
|| !input
->length
)
1082 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1084 r
->to_work_format_buf
.index
= 0;
1085 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1087 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1088 if (r
->to_work_format_buf
.memblock
)
1089 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1091 r
->to_work_format_buf_samples
= n_samples
;
1092 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1095 src
= pa_memblock_acquire_chunk(input
);
1096 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1098 r
->to_work_format_func(n_samples
, src
, dst
);
1100 pa_memblock_release(input
->memblock
);
1101 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1103 return &r
->to_work_format_buf
;
1106 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1107 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1109 size_t leftover_length
= 0;
1114 pa_assert(input
->memblock
);
1116 /* Remap channels and place the result in remap_buf. There may be leftover
1117 * data in the beginning of remap_buf. The leftover data is already
1118 * remapped, so it's not part of the input, it's part of the output. */
1120 have_leftover
= r
->remap_buf_contains_leftover_data
;
1121 r
->remap_buf_contains_leftover_data
= false;
1123 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1125 else if (input
->length
<= 0)
1126 return &r
->remap_buf
;
1128 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1129 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1131 if (have_leftover
) {
1132 leftover_length
= r
->remap_buf
.length
;
1133 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1136 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1137 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1139 if (have_leftover
) {
1140 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1141 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1143 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1144 dst
= pa_memblock_acquire(new_block
);
1145 memcpy(dst
, src
, leftover_length
);
1146 pa_memblock_release(r
->remap_buf
.memblock
);
1147 pa_memblock_release(new_block
);
1149 pa_memblock_unref(r
->remap_buf
.memblock
);
1150 r
->remap_buf
.memblock
= new_block
;
1151 r
->remap_buf_size
= r
->remap_buf
.length
;
1155 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1156 if (r
->remap_buf
.memblock
)
1157 pa_memblock_unref(r
->remap_buf
.memblock
);
1159 r
->remap_buf_size
= r
->remap_buf
.length
;
1160 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1164 src
= pa_memblock_acquire_chunk(input
);
1165 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1167 if (r
->map_required
) {
1168 pa_remap_t
*remap
= &r
->remap
;
1170 pa_assert(remap
->do_remap
);
1171 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1174 memcpy(dst
, src
, input
->length
);
1176 pa_memblock_release(input
->memblock
);
1177 pa_memblock_release(r
->remap_buf
.memblock
);
1179 return &r
->remap_buf
;
1182 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1183 unsigned in_n_frames
, in_n_samples
;
1184 unsigned out_n_frames
, out_n_samples
;
1189 /* Resample the data and place the result in resample_buf. */
1191 if (!r
->impl_resample
|| !input
->length
)
1194 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1195 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1197 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1198 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1200 r
->resample_buf
.index
= 0;
1201 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1203 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1204 if (r
->resample_buf
.memblock
)
1205 pa_memblock_unref(r
->resample_buf
.memblock
);
1207 r
->resample_buf_samples
= out_n_samples
;
1208 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1211 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1212 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1214 return &r
->resample_buf
;
1217 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1218 unsigned n_samples
, n_frames
;
1224 /* Convert the data into the correct sample type and place the result in
1225 * from_work_format_buf. */
1227 if (!r
->from_work_format_func
|| !input
->length
)
1230 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1231 n_frames
= n_samples
/ r
->o_ss
.channels
;
1233 r
->from_work_format_buf
.index
= 0;
1234 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1236 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1237 if (r
->from_work_format_buf
.memblock
)
1238 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1240 r
->from_work_format_buf_samples
= n_samples
;
1241 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1244 src
= pa_memblock_acquire_chunk(input
);
1245 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1246 r
->from_work_format_func(n_samples
, src
, dst
);
1247 pa_memblock_release(input
->memblock
);
1248 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1250 return &r
->from_work_format_buf
;
1253 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1259 pa_assert(in
->length
);
1260 pa_assert(in
->memblock
);
1261 pa_assert(in
->length
% r
->i_fz
== 0);
1263 buf
= (pa_memchunk
*) in
;
1264 buf
= convert_to_work_format(r
, buf
);
1265 buf
= remap_channels(r
, buf
);
1266 buf
= resample(r
, buf
);
1269 buf
= convert_from_work_format(r
, buf
);
1273 pa_memblock_ref(buf
->memblock
);
1275 pa_memchunk_reset(buf
);
1277 pa_memchunk_reset(out
);
1280 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1287 /* Store the leftover to remap_buf. */
1289 r
->remap_buf
.length
= len
;
1291 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1292 if (r
->remap_buf
.memblock
)
1293 pa_memblock_unref(r
->remap_buf
.memblock
);
1295 r
->remap_buf_size
= r
->remap_buf
.length
;
1296 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1299 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1300 memcpy(dst
, buf
, r
->remap_buf
.length
);
1301 pa_memblock_release(r
->remap_buf
.memblock
);
1303 r
->remap_buf_contains_leftover_data
= true;
1306 /*** libsamplerate based implementation ***/
1308 #ifdef HAVE_LIBSAMPLERATE
1309 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1311 struct src_data
*libsamplerate_data
;
1316 pa_assert(out_n_frames
);
1318 libsamplerate_data
= r
->impl_data
;
1319 memset(&data
, 0, sizeof(data
));
1321 data
.data_in
= pa_memblock_acquire_chunk(input
);
1322 data
.input_frames
= (long int) in_n_frames
;
1324 data
.data_out
= pa_memblock_acquire_chunk(output
);
1325 data
.output_frames
= (long int) *out_n_frames
;
1327 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1328 data
.end_of_input
= 0;
1330 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1332 if (data
.input_frames_used
< in_n_frames
) {
1333 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1334 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1336 save_leftover(r
, leftover_data
, leftover_length
);
1339 pa_memblock_release(input
->memblock
);
1340 pa_memblock_release(output
->memblock
);
1342 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1345 static void libsamplerate_update_rates(pa_resampler
*r
) {
1346 struct src_data
*libsamplerate_data
;
1349 libsamplerate_data
= r
->impl_data
;
1350 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1353 static void libsamplerate_reset(pa_resampler
*r
) {
1354 struct src_data
*libsamplerate_data
;
1357 libsamplerate_data
= r
->impl_data
;
1358 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1361 static void libsamplerate_free(pa_resampler
*r
) {
1362 struct src_data
*libsamplerate_data
;
1365 libsamplerate_data
= r
->impl_data
;
1366 if (libsamplerate_data
->state
)
1367 src_delete(libsamplerate_data
->state
);
1370 static int libsamplerate_init(pa_resampler
*r
) {
1372 struct src_data
*libsamplerate_data
;
1376 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1378 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1381 r
->impl_free
= libsamplerate_free
;
1382 r
->impl_update_rates
= libsamplerate_update_rates
;
1383 r
->impl_resample
= libsamplerate_resample
;
1384 r
->impl_reset
= libsamplerate_reset
;
1385 r
->impl_data
= libsamplerate_data
;
1392 /*** speex based implementation ***/
1394 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1396 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1397 struct speex_data
*speex_data
;
1402 pa_assert(out_n_frames
);
1404 speex_data
= r
->impl_data
;
1406 in
= pa_memblock_acquire_chunk(input
);
1407 out
= pa_memblock_acquire_chunk(output
);
1409 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1411 pa_memblock_release(input
->memblock
);
1412 pa_memblock_release(output
->memblock
);
1414 pa_assert(inf
== in_n_frames
);
1415 *out_n_frames
= outf
;
1418 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1420 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1421 struct speex_data
*speex_data
;
1426 pa_assert(out_n_frames
);
1428 speex_data
= r
->impl_data
;
1430 in
= pa_memblock_acquire_chunk(input
);
1431 out
= pa_memblock_acquire_chunk(output
);
1433 pa_assert_se(speex_resampler_process_interleaved_int(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1435 pa_memblock_release(input
->memblock
);
1436 pa_memblock_release(output
->memblock
);
1438 pa_assert(inf
== in_n_frames
);
1439 *out_n_frames
= outf
;
1442 static void speex_update_rates(pa_resampler
*r
) {
1443 struct speex_data
*speex_data
;
1446 speex_data
= r
->impl_data
;
1448 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1451 static void speex_reset(pa_resampler
*r
) {
1452 struct speex_data
*speex_data
;
1455 speex_data
= r
->impl_data
;
1457 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1460 static void speex_free(pa_resampler
*r
) {
1461 struct speex_data
*speex_data
;
1464 speex_data
= r
->impl_data
;
1465 if (!speex_data
->state
)
1468 speex_resampler_destroy(speex_data
->state
);
1471 static int speex_init(pa_resampler
*r
) {
1473 struct speex_data
*speex_data
;
1477 speex_data
= pa_xnew(struct speex_data
, 1);
1479 r
->impl_free
= speex_free
;
1480 r
->impl_update_rates
= speex_update_rates
;
1481 r
->impl_reset
= speex_reset
;
1482 r
->impl_data
= speex_data
;
1484 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1486 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1487 r
->impl_resample
= speex_resample_int
;
1490 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1492 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1493 r
->impl_resample
= speex_resample_float
;
1496 pa_log_info("Choosing speex quality setting %i.", q
);
1498 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1505 /* Trivial implementation */
1507 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1509 unsigned i_index
, o_index
;
1511 struct trivial_data
*trivial_data
;
1516 pa_assert(out_n_frames
);
1518 trivial_data
= r
->impl_data
;
1519 fz
= r
->w_sz
* r
->o_ss
.channels
;
1521 src
= pa_memblock_acquire_chunk(input
);
1522 dst
= pa_memblock_acquire_chunk(output
);
1524 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1525 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1526 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1528 if (i_index
>= in_n_frames
)
1531 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1533 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1536 pa_memblock_release(input
->memblock
);
1537 pa_memblock_release(output
->memblock
);
1539 *out_n_frames
= o_index
;
1541 trivial_data
->i_counter
+= in_n_frames
;
1543 /* Normalize counters */
1544 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1545 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1547 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1548 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1552 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1553 struct trivial_data
*trivial_data
;
1556 trivial_data
= r
->impl_data
;
1558 trivial_data
->i_counter
= 0;
1559 trivial_data
->o_counter
= 0;
1562 static int trivial_init(pa_resampler
*r
) {
1563 struct trivial_data
*trivial_data
;
1566 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1568 r
->impl_resample
= trivial_resample
;
1569 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1570 r
->impl_reset
= trivial_update_rates_or_reset
;
1571 r
->impl_data
= trivial_data
;
1576 /* Peak finder implementation */
1578 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1579 unsigned c
, o_index
= 0;
1580 unsigned i
, i_end
= 0;
1582 struct peaks_data
*peaks_data
;
1587 pa_assert(out_n_frames
);
1589 peaks_data
= r
->impl_data
;
1590 src
= pa_memblock_acquire_chunk(input
);
1591 dst
= pa_memblock_acquire_chunk(output
);
1593 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1594 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1596 while (i_end
< in_n_frames
) {
1597 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1598 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1600 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1602 /* 1ch float is treated separately, because that is the common case */
1603 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1604 float *s
= (float*) src
+ i
;
1605 float *d
= (float*) dst
+ o_index
;
1607 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1608 float n
= fabsf(*s
++);
1610 if (n
> peaks_data
->max_f
[0])
1611 peaks_data
->max_f
[0] = n
;
1615 *d
= peaks_data
->max_f
[0];
1616 peaks_data
->max_f
[0] = 0;
1617 o_index
++, peaks_data
->o_counter
++;
1619 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1620 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1621 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1623 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1624 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1625 int16_t n
= abs(*s
++);
1627 if (n
> peaks_data
->max_i
[c
])
1628 peaks_data
->max_i
[c
] = n
;
1632 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1633 *d
= peaks_data
->max_i
[c
];
1634 peaks_data
->max_i
[c
] = 0;
1636 o_index
++, peaks_data
->o_counter
++;
1639 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1640 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1642 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1643 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1644 float n
= fabsf(*s
++);
1646 if (n
> peaks_data
->max_f
[c
])
1647 peaks_data
->max_f
[c
] = n
;
1651 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1652 *d
= peaks_data
->max_f
[c
];
1653 peaks_data
->max_f
[c
] = 0;
1655 o_index
++, peaks_data
->o_counter
++;
1660 pa_memblock_release(input
->memblock
);
1661 pa_memblock_release(output
->memblock
);
1663 *out_n_frames
= o_index
;
1665 peaks_data
->i_counter
+= in_n_frames
;
1667 /* Normalize counters */
1668 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1669 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1671 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1672 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1676 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1677 struct peaks_data
*peaks_data
;
1680 peaks_data
= r
->impl_data
;
1682 peaks_data
->i_counter
= 0;
1683 peaks_data
->o_counter
= 0;
1686 static int peaks_init(pa_resampler
*r
) {
1687 struct peaks_data
*peaks_data
;
1689 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1690 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1692 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1694 r
->impl_resample
= peaks_resample
;
1695 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1696 r
->impl_reset
= peaks_update_rates_or_reset
;
1697 r
->impl_data
= peaks_data
;
1702 /*** ffmpeg based implementation ***/
1704 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1705 unsigned used_frames
= 0, c
;
1706 int previous_consumed_frames
= -1;
1707 struct ffmpeg_data
*ffmpeg_data
;
1712 pa_assert(out_n_frames
);
1714 ffmpeg_data
= r
->impl_data
;
1716 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1719 int16_t *p
, *t
, *k
, *q
, *s
;
1720 int consumed_frames
;
1722 /* Allocate a new block */
1723 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1724 p
= pa_memblock_acquire(b
);
1726 /* Now copy the input data, splitting up channels */
1727 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1729 for (u
= 0; u
< in_n_frames
; u
++) {
1731 t
+= r
->o_ss
.channels
;
1734 pa_memblock_release(input
->memblock
);
1736 /* Allocate buffer for the result */
1737 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1738 q
= pa_memblock_acquire(w
);
1741 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1744 (int) in_n_frames
, (int) *out_n_frames
,
1745 c
>= (unsigned) (r
->o_ss
.channels
-1));
1747 pa_memblock_release(b
);
1748 pa_memblock_unref(b
);
1750 pa_assert(consumed_frames
<= (int) in_n_frames
);
1751 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1752 previous_consumed_frames
= consumed_frames
;
1754 /* And place the results in the output buffer */
1755 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1756 for (u
= 0; u
< used_frames
; u
++) {
1759 s
+= r
->o_ss
.channels
;
1761 pa_memblock_release(output
->memblock
);
1762 pa_memblock_release(w
);
1763 pa_memblock_unref(w
);
1766 if (previous_consumed_frames
< (int) in_n_frames
) {
1767 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1768 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1770 save_leftover(r
, leftover_data
, leftover_length
);
1771 pa_memblock_release(input
->memblock
);
1774 *out_n_frames
= used_frames
;
1777 static void ffmpeg_free(pa_resampler
*r
) {
1779 struct ffmpeg_data
*ffmpeg_data
;
1783 ffmpeg_data
= r
->impl_data
;
1784 if (ffmpeg_data
->state
)
1785 av_resample_close(ffmpeg_data
->state
);
1787 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1788 if (ffmpeg_data
->buf
[c
].memblock
)
1789 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1792 static int ffmpeg_init(pa_resampler
*r
) {
1794 struct ffmpeg_data
*ffmpeg_data
;
1798 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1800 /* We could probably implement different quality levels by
1801 * adjusting the filter parameters here. However, ffmpeg
1802 * internally only uses these hardcoded values, so let's use them
1803 * here for now as well until ffmpeg makes this configurable. */
1805 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1808 r
->impl_free
= ffmpeg_free
;
1809 r
->impl_resample
= ffmpeg_resample
;
1810 r
->impl_data
= (void *) ffmpeg_data
;
1812 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1813 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1818 /*** copy (noop) implementation ***/
1820 static int copy_init(pa_resampler
*r
) {
1823 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);