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
;
70 uint8_t work_channels
;
72 pa_convert_func_t to_work_format_func
;
73 pa_convert_func_t from_work_format_func
;
78 void (*impl_free
)(pa_resampler
*r
);
79 void (*impl_update_rates
)(pa_resampler
*r
);
80 void (*impl_resample
)(pa_resampler
*r
, const pa_memchunk
*in
, unsigned in_samples
, pa_memchunk
*out
, unsigned *out_samples
);
81 void (*impl_reset
)(pa_resampler
*r
);
83 struct { /* data specific to the trivial resampler */
88 struct { /* data specific to the peak finder pseudo resampler */
92 float max_f
[PA_CHANNELS_MAX
];
93 int16_t max_i
[PA_CHANNELS_MAX
];
97 #ifdef HAVE_LIBSAMPLERATE
98 struct { /* data specific to libsamplerate */
104 struct { /* data specific to speex */
105 SpeexResamplerState
* state
;
109 struct { /* data specific to ffmpeg */
110 struct AVResampleContext
*state
;
111 pa_memchunk buf
[PA_CHANNELS_MAX
];
115 static int copy_init(pa_resampler
*r
);
116 static int trivial_init(pa_resampler
*r
);
118 static int speex_init(pa_resampler
*r
);
120 static int ffmpeg_init(pa_resampler
*r
);
121 static int peaks_init(pa_resampler
*r
);
122 #ifdef HAVE_LIBSAMPLERATE
123 static int libsamplerate_init(pa_resampler
*r
);
126 static void calc_map_table(pa_resampler
*r
);
128 static int (* const init_table
[])(pa_resampler
*r
) = {
129 #ifdef HAVE_LIBSAMPLERATE
130 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
131 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
132 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
133 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
134 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
136 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
137 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
138 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
139 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
140 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
142 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
165 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
176 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
177 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
187 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
188 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
190 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
191 [PA_RESAMPLER_AUTO
] = NULL
,
192 [PA_RESAMPLER_COPY
] = copy_init
,
193 [PA_RESAMPLER_PEAKS
] = peaks_init
,
196 pa_resampler
* pa_resampler_new(
198 const pa_sample_spec
*a
,
199 const pa_channel_map
*am
,
200 const pa_sample_spec
*b
,
201 const pa_channel_map
*bm
,
202 pa_resample_method_t method
,
203 pa_resample_flags_t flags
) {
205 pa_resampler
*r
= NULL
;
210 pa_assert(pa_sample_spec_valid(a
));
211 pa_assert(pa_sample_spec_valid(b
));
212 pa_assert(method
>= 0);
213 pa_assert(method
< PA_RESAMPLER_MAX
);
217 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && a
->rate
== b
->rate
) {
218 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
219 method
= PA_RESAMPLER_COPY
;
222 if (!pa_resample_method_supported(method
)) {
223 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
224 method
= PA_RESAMPLER_AUTO
;
227 if (method
== PA_RESAMPLER_FFMPEG
&& (flags
& PA_RESAMPLER_VARIABLE_RATE
)) {
228 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
229 method
= PA_RESAMPLER_AUTO
;
232 if (method
== PA_RESAMPLER_COPY
&& ((flags
& PA_RESAMPLER_VARIABLE_RATE
) || a
->rate
!= b
->rate
)) {
233 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
234 method
= PA_RESAMPLER_AUTO
;
237 if (method
== PA_RESAMPLER_AUTO
) {
239 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 3;
241 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
242 method
= PA_RESAMPLER_TRIVIAL
;
244 method
= PA_RESAMPLER_FFMPEG
;
248 r
= pa_xnew0(pa_resampler
, 1);
253 /* Fill sample specs */
257 /* set up the remap structure */
258 r
->remap
.i_ss
= &r
->i_ss
;
259 r
->remap
.o_ss
= &r
->o_ss
;
260 r
->remap
.format
= &r
->work_format
;
264 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
269 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
272 r
->i_fz
= pa_frame_size(a
);
273 r
->o_fz
= pa_frame_size(b
);
277 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
279 if ((method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
280 (method
== PA_RESAMPLER_FFMPEG
))
281 r
->work_format
= PA_SAMPLE_S16NE
;
282 else if (method
== PA_RESAMPLER_TRIVIAL
|| method
== PA_RESAMPLER_COPY
|| method
== PA_RESAMPLER_PEAKS
) {
284 if (r
->map_required
|| a
->format
!= b
->format
|| method
== PA_RESAMPLER_PEAKS
) {
286 if (a
->format
== PA_SAMPLE_S16NE
|| b
->format
== PA_SAMPLE_S16NE
)
287 r
->work_format
= PA_SAMPLE_S16NE
;
288 else if (a
->format
== PA_SAMPLE_S32NE
|| a
->format
== PA_SAMPLE_S32RE
||
289 a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
||
290 a
->format
== PA_SAMPLE_S24NE
|| a
->format
== PA_SAMPLE_S24RE
||
291 a
->format
== PA_SAMPLE_S24_32NE
|| a
->format
== PA_SAMPLE_S24_32RE
||
292 b
->format
== PA_SAMPLE_S32NE
|| b
->format
== PA_SAMPLE_S32RE
||
293 b
->format
== PA_SAMPLE_FLOAT32NE
|| b
->format
== PA_SAMPLE_FLOAT32RE
||
294 b
->format
== PA_SAMPLE_S24NE
|| b
->format
== PA_SAMPLE_S24RE
||
295 b
->format
== PA_SAMPLE_S24_32NE
|| b
->format
== PA_SAMPLE_S24_32RE
)
296 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
298 r
->work_format
= PA_SAMPLE_S16NE
;
301 r
->work_format
= a
->format
;
304 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
306 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
308 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
310 if (r
->i_ss
.format
!= r
->work_format
) {
311 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
312 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
315 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
316 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
321 if (r
->o_ss
.format
!= r
->work_format
) {
322 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
323 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
326 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
327 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
332 if (r
->o_ss
.channels
<= r
->i_ss
.channels
)
333 r
->work_channels
= r
->o_ss
.channels
;
335 r
->work_channels
= r
->i_ss
.channels
;
337 pa_log_debug("Resampler:\n rate %d -> %d (method %s),\n format %s -> %s (intermediate %s),\n channels %d -> %d (resampling %d)",
338 a
->rate
, b
->rate
, pa_resample_method_to_string(r
->method
),
339 pa_sample_format_to_string(a
->format
), pa_sample_format_to_string(b
->format
), pa_sample_format_to_string(r
->work_format
),
340 a
->channels
, b
->channels
, r
->work_channels
);
342 /* initialize implementation */
343 if (init_table
[method
](r
) < 0)
354 void pa_resampler_free(pa_resampler
*r
) {
360 if (r
->to_work_format_buf
.memblock
)
361 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
362 if (r
->remap_buf
.memblock
)
363 pa_memblock_unref(r
->remap_buf
.memblock
);
364 if (r
->resample_buf
.memblock
)
365 pa_memblock_unref(r
->resample_buf
.memblock
);
366 if (r
->from_work_format_buf
.memblock
)
367 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
372 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
376 if (r
->i_ss
.rate
== rate
)
381 r
->impl_update_rates(r
);
384 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
388 if (r
->o_ss
.rate
== rate
)
393 r
->impl_update_rates(r
);
396 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
399 /* Let's round up here to make it more likely that the caller will get at
400 * least out_length amount of data from pa_resampler_run().
402 * We don't take the leftover into account here. If we did, then it might
403 * be in theory possible that this function would return 0 and
404 * pa_resampler_run() would also return 0. That could lead to infinite
405 * loops. When the leftover is ignored here, such loops would eventually
406 * terminate, because the leftover would grow each round, finally
407 * surpassing the minimum input threshold of the resampler. */
408 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
;
411 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
416 /* Let's round up here to ensure that the caller will always allocate big
417 * enough output buffer. */
419 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
421 if (r
->remap_buf_contains_leftover_data
)
422 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
424 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
427 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
428 size_t block_size_max
;
429 pa_sample_spec max_ss
;
435 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
437 /* We deduce the "largest" sample spec we're using during the
439 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
441 /* We silently assume that the format enum is ordered by size */
442 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
443 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
445 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
447 max_fs
= pa_frame_size(&max_ss
);
448 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
450 if (r
->remap_buf_contains_leftover_data
)
451 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
453 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
456 void pa_resampler_reset(pa_resampler
*r
) {
462 r
->remap_buf_contains_leftover_data
= false;
465 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
471 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
477 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
483 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
489 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
495 static const char * const resample_methods
[] = {
496 "src-sinc-best-quality",
497 "src-sinc-medium-quality",
499 "src-zero-order-hold",
530 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
532 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
535 return resample_methods
[m
];
538 int pa_resample_method_supported(pa_resample_method_t m
) {
540 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
543 #ifndef HAVE_LIBSAMPLERATE
544 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
549 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
551 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
558 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
559 pa_resample_method_t m
;
563 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
564 if (pa_streq(string
, resample_methods
[m
]))
567 if (pa_streq(string
, "speex-fixed"))
568 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 3;
570 if (pa_streq(string
, "speex-float"))
571 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 3;
573 return PA_RESAMPLER_INVALID
;
576 static bool on_left(pa_channel_position_t p
) {
579 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
580 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
581 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
582 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
583 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
584 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
587 static bool on_right(pa_channel_position_t p
) {
590 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
591 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
592 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
593 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
594 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
595 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
598 static bool on_center(pa_channel_position_t p
) {
601 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
602 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
603 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
604 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
605 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
608 static bool on_lfe(pa_channel_position_t p
) {
610 p
== PA_CHANNEL_POSITION_LFE
;
613 static bool on_front(pa_channel_position_t p
) {
615 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
616 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
617 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
618 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
619 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
620 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
621 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
622 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
625 static bool on_rear(pa_channel_position_t p
) {
627 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
628 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
629 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
630 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
631 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
632 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
635 static bool on_side(pa_channel_position_t p
) {
637 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
638 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
639 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
649 static int front_rear_side(pa_channel_position_t p
) {
659 static void calc_map_table(pa_resampler
*r
) {
662 bool ic_connected
[PA_CHANNELS_MAX
];
670 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
)))))
675 n_oc
= r
->o_ss
.channels
;
676 n_ic
= r
->i_ss
.channels
;
678 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
679 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
681 memset(ic_connected
, 0, sizeof(ic_connected
));
682 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
684 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
687 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
688 m
->map_table_f
[oc
][oc
] = 1.0f
;
690 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
692 for (oc
= 0; oc
< n_oc
; oc
++) {
693 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
695 for (ic
= 0; ic
< n_ic
; ic
++) {
696 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
698 /* We shall not do any remixing. Hence, just check by name */
700 m
->map_table_f
[oc
][ic
] = 1.0f
;
705 /* OK, we shall do the full monty: upmixing and downmixing. Our
706 * algorithm is relatively simple, does not do spacialization, delay
707 * elements or apply lowpass filters for LFE. Patches are always
708 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
709 * probably wouldn't make any sense anyway.)
711 * This code is not idempotent: downmixing an upmixed stereo stream is
712 * not identical to the original. The volume will not match, and the
713 * two channels will be a linear combination of both.
715 * This is loosely based on random suggestions found on the Internet,
717 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
720 * The algorithm works basically like this:
722 * 1) Connect all channels with matching names.
725 * S:Mono: Copy into all D:channels
726 * D:Mono: Avg all S:channels
728 * 3) Mix D:Left, D:Right:
729 * D:Left: If not connected, avg all S:Left
730 * D:Right: If not connected, avg all S:Right
733 * If not connected, avg all S:Center
734 * If still not connected, avg all S:Left, S:Right
737 * If not connected, avg all S:*
739 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
740 * connected, mix into all D:left and all D:right channels. Gain is
743 * 7) Make sure S:Center, S:LFE is used:
745 * S:Center, S:LFE: If not connected, mix into all D:left, all
746 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
747 * for LFE. C-front is only mixed into L-front/R-front if available,
748 * otherwise into all L/R channels. Similarly for C-rear.
750 * 8) Normalize each row in the matrix such that the sum for each row is
751 * not larger than 1.0 in order to avoid clipping.
753 * S: and D: shall relate to the source resp. destination channels.
755 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
756 * rear if needed. For 4: we try to find some suitable C source for C,
757 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
758 * channels. For 6: the rear channels should not be dropped entirely,
759 * however have only minimal impact. For 7: movies usually encode
760 * speech on the center channel. Thus we have to make sure this channel
761 * is distributed to L and R if not available in the output. Also, LFE
762 * is used to achieve a greater dynamic range, and thus we should try
763 * to do our best to pass it to L+R.
770 ic_unconnected_left
= 0,
771 ic_unconnected_right
= 0,
772 ic_unconnected_center
= 0,
773 ic_unconnected_lfe
= 0;
774 bool ic_unconnected_center_mixed_in
= 0;
778 for (ic
= 0; ic
< n_ic
; ic
++) {
779 if (on_left(r
->i_cm
.map
[ic
]))
781 if (on_right(r
->i_cm
.map
[ic
]))
783 if (on_center(r
->i_cm
.map
[ic
]))
787 for (oc
= 0; oc
< n_oc
; oc
++) {
788 bool oc_connected
= false;
789 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
791 for (ic
= 0; ic
< n_ic
; ic
++) {
792 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
794 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
795 m
->map_table_f
[oc
][ic
] = 1.0f
;
798 ic_connected
[ic
] = true;
800 else if (b
== PA_CHANNEL_POSITION_MONO
) {
801 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
804 ic_connected
[ic
] = true;
809 /* Try to find matching input ports for this output port */
813 /* We are not connected and on the left side, let's
814 * average all left side input channels. */
817 for (ic
= 0; ic
< n_ic
; ic
++)
818 if (on_left(r
->i_cm
.map
[ic
])) {
819 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
820 ic_connected
[ic
] = true;
823 /* We ignore the case where there is no left input channel.
824 * Something is really wrong in this case anyway. */
826 } else if (on_right(b
)) {
828 /* We are not connected and on the right side, let's
829 * average all right side input channels. */
832 for (ic
= 0; ic
< n_ic
; ic
++)
833 if (on_right(r
->i_cm
.map
[ic
])) {
834 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
835 ic_connected
[ic
] = true;
838 /* We ignore the case where there is no right input
839 * channel. Something is really wrong in this case anyway.
842 } else if (on_center(b
)) {
846 /* We are not connected and at the center. Let's average
847 * all center input channels. */
849 for (ic
= 0; ic
< n_ic
; ic
++)
850 if (on_center(r
->i_cm
.map
[ic
])) {
851 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
852 ic_connected
[ic
] = true;
855 } else if (ic_left
+ ic_right
> 0) {
857 /* Hmm, no center channel around, let's synthesize it
858 * by mixing L and R.*/
860 for (ic
= 0; ic
< n_ic
; ic
++)
861 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
862 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
863 ic_connected
[ic
] = true;
867 /* We ignore the case where there is not even a left or
868 * right input channel. Something is really wrong in this
871 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
873 /* We are not connected and an LFE. Let's average all
874 * channels for LFE. */
876 for (ic
= 0; ic
< n_ic
; ic
++)
877 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
879 /* Please note that a channel connected to LFE doesn't
880 * really count as connected. */
885 for (ic
= 0; ic
< n_ic
; ic
++) {
886 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
888 if (ic_connected
[ic
])
892 ic_unconnected_left
++;
893 else if (on_right(a
))
894 ic_unconnected_right
++;
895 else if (on_center(a
))
896 ic_unconnected_center
++;
898 ic_unconnected_lfe
++;
901 for (ic
= 0; ic
< n_ic
; ic
++) {
902 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
904 if (ic_connected
[ic
])
907 for (oc
= 0; oc
< n_oc
; oc
++) {
908 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
910 if (on_left(a
) && on_left(b
))
911 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
913 else if (on_right(a
) && on_right(b
))
914 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
916 else if (on_center(a
) && on_center(b
)) {
917 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
918 ic_unconnected_center_mixed_in
= true;
920 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
921 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
925 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
926 unsigned ncenter
[PA_CHANNELS_MAX
];
927 bool found_frs
[PA_CHANNELS_MAX
];
929 memset(ncenter
, 0, sizeof(ncenter
));
930 memset(found_frs
, 0, sizeof(found_frs
));
932 /* Hmm, as it appears there was no center channel we
933 could mix our center channel in. In this case, mix it into
934 left and right. Using .5 as the factor. */
936 for (ic
= 0; ic
< n_ic
; ic
++) {
938 if (ic_connected
[ic
])
941 if (!on_center(r
->i_cm
.map
[ic
]))
944 for (oc
= 0; oc
< n_oc
; oc
++) {
946 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
949 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
950 found_frs
[ic
] = true;
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 (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
965 for (oc
= 0; oc
< n_oc
; oc
++) {
967 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
970 if (ncenter
[oc
] <= 0)
973 for (ic
= 0; ic
< n_ic
; ic
++) {
975 if (!on_center(r
->i_cm
.map
[ic
]))
978 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
979 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
985 for (oc
= 0; oc
< n_oc
; oc
++) {
987 for (ic
= 0; ic
< n_ic
; ic
++)
988 sum
+= m
->map_table_f
[oc
][ic
];
991 for (ic
= 0; ic
< n_ic
; ic
++)
992 m
->map_table_f
[oc
][ic
] /= sum
;
995 /* make an 16:16 int version of the matrix */
996 for (oc
= 0; oc
< n_oc
; oc
++)
997 for (ic
= 0; ic
< n_ic
; ic
++)
998 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1000 s
= pa_strbuf_new();
1002 pa_strbuf_printf(s
, " ");
1003 for (ic
= 0; ic
< n_ic
; ic
++)
1004 pa_strbuf_printf(s
, " I%02u ", ic
);
1005 pa_strbuf_puts(s
, "\n +");
1007 for (ic
= 0; ic
< n_ic
; ic
++)
1008 pa_strbuf_printf(s
, "------");
1009 pa_strbuf_puts(s
, "\n");
1011 for (oc
= 0; oc
< n_oc
; oc
++) {
1012 pa_strbuf_printf(s
, "O%02u |", oc
);
1014 for (ic
= 0; ic
< n_ic
; ic
++)
1015 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1017 pa_strbuf_puts(s
, "\n");
1020 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1023 /* initialize the remapping function */
1027 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1033 pa_assert(input
->memblock
);
1035 /* Convert the incoming sample into the work sample format and place them
1036 * in to_work_format_buf. */
1038 if (!r
->to_work_format_func
|| !input
->length
)
1041 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1043 r
->to_work_format_buf
.index
= 0;
1044 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1046 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1047 if (r
->to_work_format_buf
.memblock
)
1048 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1050 r
->to_work_format_buf_samples
= n_samples
;
1051 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1054 src
= pa_memblock_acquire_chunk(input
);
1055 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1057 r
->to_work_format_func(n_samples
, src
, dst
);
1059 pa_memblock_release(input
->memblock
);
1060 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1062 return &r
->to_work_format_buf
;
1065 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1066 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1068 size_t leftover_length
= 0;
1073 pa_assert(input
->memblock
);
1075 /* Remap channels and place the result in remap_buf. There may be leftover
1076 * data in the beginning of remap_buf. The leftover data is already
1077 * remapped, so it's not part of the input, it's part of the output. */
1079 have_leftover
= r
->remap_buf_contains_leftover_data
;
1080 r
->remap_buf_contains_leftover_data
= false;
1082 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1084 else if (input
->length
<= 0)
1085 return &r
->remap_buf
;
1087 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1088 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1090 if (have_leftover
) {
1091 leftover_length
= r
->remap_buf
.length
;
1092 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1095 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1096 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1098 if (have_leftover
) {
1099 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1100 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1102 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1103 dst
= pa_memblock_acquire(new_block
);
1104 memcpy(dst
, src
, leftover_length
);
1105 pa_memblock_release(r
->remap_buf
.memblock
);
1106 pa_memblock_release(new_block
);
1108 pa_memblock_unref(r
->remap_buf
.memblock
);
1109 r
->remap_buf
.memblock
= new_block
;
1110 r
->remap_buf_size
= r
->remap_buf
.length
;
1114 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1115 if (r
->remap_buf
.memblock
)
1116 pa_memblock_unref(r
->remap_buf
.memblock
);
1118 r
->remap_buf_size
= r
->remap_buf
.length
;
1119 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1123 src
= pa_memblock_acquire_chunk(input
);
1124 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1126 if (r
->map_required
) {
1127 pa_remap_t
*remap
= &r
->remap
;
1129 pa_assert(remap
->do_remap
);
1130 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1133 memcpy(dst
, src
, input
->length
);
1135 pa_memblock_release(input
->memblock
);
1136 pa_memblock_release(r
->remap_buf
.memblock
);
1138 return &r
->remap_buf
;
1141 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1142 unsigned in_n_frames
, in_n_samples
;
1143 unsigned out_n_frames
, out_n_samples
;
1148 /* Resample the data and place the result in resample_buf. */
1150 if (!r
->impl_resample
|| !input
->length
)
1153 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1154 in_n_frames
= (unsigned) (in_n_samples
/ r
->work_channels
);
1156 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1157 out_n_samples
= out_n_frames
* r
->work_channels
;
1159 r
->resample_buf
.index
= 0;
1160 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1162 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1163 if (r
->resample_buf
.memblock
)
1164 pa_memblock_unref(r
->resample_buf
.memblock
);
1166 r
->resample_buf_samples
= out_n_samples
;
1167 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1170 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1171 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->work_channels
;
1173 return &r
->resample_buf
;
1176 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1177 unsigned n_samples
, n_frames
;
1183 /* Convert the data into the correct sample type and place the result in
1184 * from_work_format_buf. */
1186 if (!r
->from_work_format_func
|| !input
->length
)
1189 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1190 n_frames
= n_samples
/ r
->o_ss
.channels
;
1192 r
->from_work_format_buf
.index
= 0;
1193 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1195 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1196 if (r
->from_work_format_buf
.memblock
)
1197 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1199 r
->from_work_format_buf_samples
= n_samples
;
1200 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1203 src
= pa_memblock_acquire_chunk(input
);
1204 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1205 r
->from_work_format_func(n_samples
, src
, dst
);
1206 pa_memblock_release(input
->memblock
);
1207 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1209 return &r
->from_work_format_buf
;
1212 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1218 pa_assert(in
->length
);
1219 pa_assert(in
->memblock
);
1220 pa_assert(in
->length
% r
->i_fz
== 0);
1222 buf
= (pa_memchunk
*) in
;
1223 buf
= convert_to_work_format(r
, buf
);
1224 /* Try to save resampling effort: if we have more output channels than
1225 * input channels, do resampling first, then remapping. */
1226 if (r
->o_ss
.channels
<= r
->i_ss
.channels
) {
1227 buf
= remap_channels(r
, buf
);
1228 buf
= resample(r
, buf
);
1230 buf
= resample(r
, buf
);
1231 buf
= remap_channels(r
, buf
);
1235 buf
= convert_from_work_format(r
, buf
);
1239 pa_memblock_ref(buf
->memblock
);
1241 pa_memchunk_reset(buf
);
1243 pa_memchunk_reset(out
);
1246 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1253 /* Store the leftover to remap_buf. */
1255 r
->remap_buf
.length
= len
;
1257 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1258 if (r
->remap_buf
.memblock
)
1259 pa_memblock_unref(r
->remap_buf
.memblock
);
1261 r
->remap_buf_size
= r
->remap_buf
.length
;
1262 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1265 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1266 memcpy(dst
, buf
, r
->remap_buf
.length
);
1267 pa_memblock_release(r
->remap_buf
.memblock
);
1269 r
->remap_buf_contains_leftover_data
= true;
1272 /*** libsamplerate based implementation ***/
1274 #ifdef HAVE_LIBSAMPLERATE
1275 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1281 pa_assert(out_n_frames
);
1283 memset(&data
, 0, sizeof(data
));
1285 data
.data_in
= pa_memblock_acquire_chunk(input
);
1286 data
.input_frames
= (long int) in_n_frames
;
1288 data
.data_out
= pa_memblock_acquire_chunk(output
);
1289 data
.output_frames
= (long int) *out_n_frames
;
1291 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1292 data
.end_of_input
= 0;
1294 pa_assert_se(src_process(r
->src
.state
, &data
) == 0);
1296 if (data
.input_frames_used
< in_n_frames
) {
1297 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->work_channels
;
1298 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->work_channels
;
1300 save_leftover(r
, leftover_data
, leftover_length
);
1303 pa_memblock_release(input
->memblock
);
1304 pa_memblock_release(output
->memblock
);
1306 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1309 static void libsamplerate_update_rates(pa_resampler
*r
) {
1312 pa_assert_se(src_set_ratio(r
->src
.state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1315 static void libsamplerate_reset(pa_resampler
*r
) {
1318 pa_assert_se(src_reset(r
->src
.state
) == 0);
1321 static void libsamplerate_free(pa_resampler
*r
) {
1325 src_delete(r
->src
.state
);
1328 static int libsamplerate_init(pa_resampler
*r
) {
1333 if (!(r
->src
.state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1336 r
->impl_free
= libsamplerate_free
;
1337 r
->impl_update_rates
= libsamplerate_update_rates
;
1338 r
->impl_resample
= libsamplerate_resample
;
1339 r
->impl_reset
= libsamplerate_reset
;
1346 /*** speex based implementation ***/
1348 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1350 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1355 pa_assert(out_n_frames
);
1357 in
= pa_memblock_acquire_chunk(input
);
1358 out
= pa_memblock_acquire_chunk(output
);
1360 pa_assert_se(speex_resampler_process_interleaved_float(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
1362 pa_memblock_release(input
->memblock
);
1363 pa_memblock_release(output
->memblock
);
1365 pa_assert(inf
== in_n_frames
);
1366 *out_n_frames
= outf
;
1369 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1371 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1376 pa_assert(out_n_frames
);
1378 in
= pa_memblock_acquire_chunk(input
);
1379 out
= pa_memblock_acquire_chunk(output
);
1381 pa_assert_se(speex_resampler_process_interleaved_int(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
1383 pa_memblock_release(input
->memblock
);
1384 pa_memblock_release(output
->memblock
);
1386 pa_assert(inf
== in_n_frames
);
1387 *out_n_frames
= outf
;
1390 static void speex_update_rates(pa_resampler
*r
) {
1393 pa_assert_se(speex_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1396 static void speex_reset(pa_resampler
*r
) {
1399 pa_assert_se(speex_resampler_reset_mem(r
->speex
.state
) == 0);
1402 static void speex_free(pa_resampler
*r
) {
1405 if (!r
->speex
.state
)
1408 speex_resampler_destroy(r
->speex
.state
);
1411 static int speex_init(pa_resampler
*r
) {
1416 r
->impl_free
= speex_free
;
1417 r
->impl_update_rates
= speex_update_rates
;
1418 r
->impl_reset
= speex_reset
;
1420 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1422 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1423 r
->impl_resample
= speex_resample_int
;
1426 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1428 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1429 r
->impl_resample
= speex_resample_float
;
1432 pa_log_info("Choosing speex quality setting %i.", q
);
1434 if (!(r
->speex
.state
= speex_resampler_init(r
->work_channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1441 /* Trivial implementation */
1443 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1445 unsigned i_index
, o_index
;
1451 pa_assert(out_n_frames
);
1453 fz
= r
->w_sz
* r
->work_channels
;
1455 src
= pa_memblock_acquire_chunk(input
);
1456 dst
= pa_memblock_acquire_chunk(output
);
1458 for (o_index
= 0;; o_index
++, r
->trivial
.o_counter
++) {
1459 i_index
= ((uint64_t) r
->trivial
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1460 i_index
= i_index
> r
->trivial
.i_counter
? i_index
- r
->trivial
.i_counter
: 0;
1462 if (i_index
>= in_n_frames
)
1465 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1467 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1470 pa_memblock_release(input
->memblock
);
1471 pa_memblock_release(output
->memblock
);
1473 *out_n_frames
= o_index
;
1475 r
->trivial
.i_counter
+= in_n_frames
;
1477 /* Normalize counters */
1478 while (r
->trivial
.i_counter
>= r
->i_ss
.rate
) {
1479 pa_assert(r
->trivial
.o_counter
>= r
->o_ss
.rate
);
1481 r
->trivial
.i_counter
-= r
->i_ss
.rate
;
1482 r
->trivial
.o_counter
-= r
->o_ss
.rate
;
1486 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1489 r
->trivial
.i_counter
= 0;
1490 r
->trivial
.o_counter
= 0;
1493 static int trivial_init(pa_resampler
*r
) {
1496 r
->trivial
.o_counter
= r
->trivial
.i_counter
= 0;
1498 r
->impl_resample
= trivial_resample
;
1499 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1500 r
->impl_reset
= trivial_update_rates_or_reset
;
1505 /* Peak finder implementation */
1507 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1508 unsigned c
, o_index
= 0;
1509 unsigned i
, i_end
= 0;
1515 pa_assert(out_n_frames
);
1517 src
= pa_memblock_acquire_chunk(input
);
1518 dst
= pa_memblock_acquire_chunk(output
);
1520 i
= ((uint64_t) r
->peaks
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1521 i
= i
> r
->peaks
.i_counter
? i
- r
->peaks
.i_counter
: 0;
1523 while (i_end
< in_n_frames
) {
1524 i_end
= ((uint64_t) (r
->peaks
.o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1525 i_end
= i_end
> r
->peaks
.i_counter
? i_end
- r
->peaks
.i_counter
: 0;
1527 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1529 /* 1ch float is treated separately, because that is the common case */
1530 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1531 float *s
= (float*) src
+ i
;
1532 float *d
= (float*) dst
+ o_index
;
1534 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1535 float n
= fabsf(*s
++);
1537 if (n
> r
->peaks
.max_f
[0])
1538 r
->peaks
.max_f
[0] = n
;
1542 *d
= r
->peaks
.max_f
[0];
1543 r
->peaks
.max_f
[0] = 0;
1544 o_index
++, r
->peaks
.o_counter
++;
1546 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1547 int16_t *s
= (int16_t*) src
+ r
->i_ss
.channels
* i
;
1548 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1550 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1551 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1552 int16_t n
= abs(*s
++);
1554 if (n
> r
->peaks
.max_i
[c
])
1555 r
->peaks
.max_i
[c
] = n
;
1559 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1560 *d
= r
->peaks
.max_i
[c
];
1561 r
->peaks
.max_i
[c
] = 0;
1563 o_index
++, r
->peaks
.o_counter
++;
1566 float *s
= (float*) src
+ r
->i_ss
.channels
* i
;
1567 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1569 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1570 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1571 float n
= fabsf(*s
++);
1573 if (n
> r
->peaks
.max_f
[c
])
1574 r
->peaks
.max_f
[c
] = n
;
1578 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1579 *d
= r
->peaks
.max_f
[c
];
1580 r
->peaks
.max_f
[c
] = 0;
1582 o_index
++, r
->peaks
.o_counter
++;
1587 pa_memblock_release(input
->memblock
);
1588 pa_memblock_release(output
->memblock
);
1590 *out_n_frames
= o_index
;
1592 r
->peaks
.i_counter
+= in_n_frames
;
1594 /* Normalize counters */
1595 while (r
->peaks
.i_counter
>= r
->i_ss
.rate
) {
1596 pa_assert(r
->peaks
.o_counter
>= r
->o_ss
.rate
);
1598 r
->peaks
.i_counter
-= r
->i_ss
.rate
;
1599 r
->peaks
.o_counter
-= r
->o_ss
.rate
;
1603 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1606 r
->peaks
.i_counter
= 0;
1607 r
->peaks
.o_counter
= 0;
1610 static int peaks_init(pa_resampler
*r
) {
1612 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1613 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1615 r
->peaks
.o_counter
= r
->peaks
.i_counter
= 0;
1616 memset(r
->peaks
.max_i
, 0, sizeof(r
->peaks
.max_i
));
1617 memset(r
->peaks
.max_f
, 0, sizeof(r
->peaks
.max_f
));
1619 r
->impl_resample
= peaks_resample
;
1620 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1621 r
->impl_reset
= peaks_update_rates_or_reset
;
1626 /*** ffmpeg based implementation ***/
1628 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1629 unsigned used_frames
= 0, c
;
1630 int previous_consumed_frames
= -1;
1635 pa_assert(out_n_frames
);
1637 for (c
= 0; c
< r
->work_channels
; c
++) {
1640 int16_t *p
, *t
, *k
, *q
, *s
;
1641 int consumed_frames
;
1643 /* Allocate a new block */
1644 b
= pa_memblock_new(r
->mempool
, r
->ffmpeg
.buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1645 p
= pa_memblock_acquire(b
);
1647 /* Now copy the input data, splitting up channels */
1648 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1650 for (u
= 0; u
< in_n_frames
; u
++) {
1652 t
+= r
->work_channels
;
1655 pa_memblock_release(input
->memblock
);
1657 /* Allocate buffer for the result */
1658 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1659 q
= pa_memblock_acquire(w
);
1662 used_frames
= (unsigned) av_resample(r
->ffmpeg
.state
,
1665 (int) in_n_frames
, (int) *out_n_frames
,
1666 c
>= (unsigned) (r
->work_channels
-1));
1668 pa_memblock_release(b
);
1669 pa_memblock_unref(b
);
1671 pa_assert(consumed_frames
<= (int) in_n_frames
);
1672 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1673 previous_consumed_frames
= consumed_frames
;
1675 /* And place the results in the output buffer */
1676 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1677 for (u
= 0; u
< used_frames
; u
++) {
1680 s
+= r
->work_channels
;
1682 pa_memblock_release(output
->memblock
);
1683 pa_memblock_release(w
);
1684 pa_memblock_unref(w
);
1687 if (previous_consumed_frames
< (int) in_n_frames
) {
1688 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1689 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1691 save_leftover(r
, leftover_data
, leftover_length
);
1692 pa_memblock_release(input
->memblock
);
1695 *out_n_frames
= used_frames
;
1698 static void ffmpeg_free(pa_resampler
*r
) {
1703 if (r
->ffmpeg
.state
)
1704 av_resample_close(r
->ffmpeg
.state
);
1706 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
1707 if (r
->ffmpeg
.buf
[c
].memblock
)
1708 pa_memblock_unref(r
->ffmpeg
.buf
[c
].memblock
);
1711 static int ffmpeg_init(pa_resampler
*r
) {
1716 /* We could probably implement different quality levels by
1717 * adjusting the filter parameters here. However, ffmpeg
1718 * internally only uses these hardcoded values, so let's use them
1719 * here for now as well until ffmpeg makes this configurable. */
1721 if (!(r
->ffmpeg
.state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1724 r
->impl_free
= ffmpeg_free
;
1725 r
->impl_resample
= ffmpeg_resample
;
1727 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
1728 pa_memchunk_reset(&r
->ffmpeg
.buf
[c
]);
1733 /*** copy (noop) implementation ***/
1735 static int copy_init(pa_resampler
*r
) {
1738 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);