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 pa_resampler_impl impl
;
80 struct trivial_data
{ /* data specific to the trivial resampler */
85 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
89 float max_f
[PA_CHANNELS_MAX
];
90 int16_t max_i
[PA_CHANNELS_MAX
];
93 #ifdef HAVE_LIBSAMPLERATE
94 struct src_data
{ /* data specific to libsamplerate */
100 struct speex_data
{ /* data specific to speex */
101 SpeexResamplerState
* state
;
105 struct ffmpeg_data
{ /* data specific to ffmpeg */
106 struct AVResampleContext
*state
;
107 pa_memchunk buf
[PA_CHANNELS_MAX
];
110 static int copy_init(pa_resampler
*r
);
111 static int trivial_init(pa_resampler
*r
);
113 static int speex_init(pa_resampler
*r
);
115 static int ffmpeg_init(pa_resampler
*r
);
116 static int peaks_init(pa_resampler
*r
);
117 #ifdef HAVE_LIBSAMPLERATE
118 static int libsamplerate_init(pa_resampler
*r
);
121 static void calc_map_table(pa_resampler
*r
);
123 static int (* const init_table
[])(pa_resampler
*r
) = {
124 #ifdef HAVE_LIBSAMPLERATE
125 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
126 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
127 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
128 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
129 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
131 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
132 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
133 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
134 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
135 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
137 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
139 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
140 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
141 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
142 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
162 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
163 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
164 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
165 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
173 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
174 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
175 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
176 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
185 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
186 [PA_RESAMPLER_AUTO
] = NULL
,
187 [PA_RESAMPLER_COPY
] = copy_init
,
188 [PA_RESAMPLER_PEAKS
] = peaks_init
,
191 static pa_resample_method_t
pa_resampler_fix_method(
192 pa_resample_flags_t flags
,
193 pa_resample_method_t method
,
194 const uint32_t rate_a
,
195 const uint32_t rate_b
) {
197 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
198 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
199 pa_assert(method
>= 0);
200 pa_assert(method
< PA_RESAMPLER_MAX
);
202 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
203 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
204 method
= PA_RESAMPLER_COPY
;
207 if (!pa_resample_method_supported(method
)) {
208 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
209 method
= PA_RESAMPLER_AUTO
;
213 case PA_RESAMPLER_COPY
:
214 if (rate_a
!= rate_b
) {
215 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
218 /* Else fall through */
219 case PA_RESAMPLER_FFMPEG
:
220 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
221 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
222 method
= PA_RESAMPLER_AUTO
;
226 /* The Peaks resampler only supports downsampling.
227 * Revert to auto if we are upsampling */
228 case PA_RESAMPLER_PEAKS
:
229 if (rate_a
< rate_b
) {
230 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
231 method
= PA_RESAMPLER_AUTO
;
239 if (method
== PA_RESAMPLER_AUTO
) {
241 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
243 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
244 method
= PA_RESAMPLER_TRIVIAL
;
246 method
= PA_RESAMPLER_FFMPEG
;
253 /* Return true if a is a more precise sample format than b, else return false */
254 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
255 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
256 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
265 case PA_SAMPLE_S16LE
:
266 case PA_SAMPLE_S16BE
:
267 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
273 case PA_SAMPLE_S24LE
:
274 case PA_SAMPLE_S24BE
:
275 case PA_SAMPLE_S24_32LE
:
276 case PA_SAMPLE_S24_32BE
:
277 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
278 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
284 case PA_SAMPLE_FLOAT32LE
:
285 case PA_SAMPLE_FLOAT32BE
:
286 case PA_SAMPLE_S32LE
:
287 case PA_SAMPLE_S32BE
:
288 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
289 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
300 static pa_sample_format_t
pa_resampler_choose_work_format(
301 pa_resample_method_t method
,
302 pa_sample_format_t a
,
303 pa_sample_format_t b
,
305 pa_sample_format_t work_format
;
307 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
308 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
309 pa_assert(method
>= 0);
310 pa_assert(method
< PA_RESAMPLER_MAX
);
312 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
313 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
316 /* This block is for resampling functions that only
317 * support the S16 sample format. */
318 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
319 case PA_RESAMPLER_FFMPEG
:
320 work_format
= PA_SAMPLE_S16NE
;
323 /* This block is for resampling functions that support
324 * any sample format. */
325 case PA_RESAMPLER_COPY
: /* fall through */
326 case PA_RESAMPLER_TRIVIAL
:
327 if (!map_required
&& a
== b
) {
331 /* Else fall trough */
332 case PA_RESAMPLER_PEAKS
:
333 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
334 work_format
= PA_SAMPLE_S16NE
;
335 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
336 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
337 work_format
= PA_SAMPLE_FLOAT32NE
;
339 work_format
= PA_SAMPLE_S16NE
;
343 work_format
= PA_SAMPLE_FLOAT32NE
;
349 pa_resampler
* pa_resampler_new(
351 const pa_sample_spec
*a
,
352 const pa_channel_map
*am
,
353 const pa_sample_spec
*b
,
354 const pa_channel_map
*bm
,
355 pa_resample_method_t method
,
356 pa_resample_flags_t flags
) {
358 pa_resampler
*r
= NULL
;
363 pa_assert(pa_sample_spec_valid(a
));
364 pa_assert(pa_sample_spec_valid(b
));
365 pa_assert(method
>= 0);
366 pa_assert(method
< PA_RESAMPLER_MAX
);
368 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
370 r
= pa_xnew0(pa_resampler
, 1);
375 /* Fill sample specs */
379 /* set up the remap structure */
380 r
->remap
.i_ss
= &r
->i_ss
;
381 r
->remap
.o_ss
= &r
->o_ss
;
382 r
->remap
.format
= &r
->work_format
;
386 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
391 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
394 r
->i_fz
= pa_frame_size(a
);
395 r
->o_fz
= pa_frame_size(b
);
399 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
401 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
403 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
405 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
407 if (r
->i_ss
.format
!= r
->work_format
) {
408 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
409 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
412 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
413 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
418 if (r
->o_ss
.format
!= r
->work_format
) {
419 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
420 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
423 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
424 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
429 /* initialize implementation */
430 if (init_table
[method
](r
) < 0)
441 void pa_resampler_free(pa_resampler
*r
) {
447 if (r
->to_work_format_buf
.memblock
)
448 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
449 if (r
->remap_buf
.memblock
)
450 pa_memblock_unref(r
->remap_buf
.memblock
);
451 if (r
->resample_buf
.memblock
)
452 pa_memblock_unref(r
->resample_buf
.memblock
);
453 if (r
->from_work_format_buf
.memblock
)
454 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
456 pa_xfree(r
->impl
.data
);
460 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
464 if (r
->i_ss
.rate
== rate
)
469 r
->impl
.update_rates(r
);
472 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
476 if (r
->o_ss
.rate
== rate
)
481 r
->impl
.update_rates(r
);
484 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
487 /* Let's round up here to make it more likely that the caller will get at
488 * least out_length amount of data from pa_resampler_run().
490 * We don't take the leftover into account here. If we did, then it might
491 * be in theory possible that this function would return 0 and
492 * pa_resampler_run() would also return 0. That could lead to infinite
493 * loops. When the leftover is ignored here, such loops would eventually
494 * terminate, because the leftover would grow each round, finally
495 * surpassing the minimum input threshold of the resampler. */
496 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
;
499 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
504 /* Let's round up here to ensure that the caller will always allocate big
505 * enough output buffer. */
507 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
509 if (r
->remap_buf_contains_leftover_data
)
510 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
512 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
515 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
516 size_t block_size_max
;
517 pa_sample_spec max_ss
;
523 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
525 /* We deduce the "largest" sample spec we're using during the
527 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
529 /* We silently assume that the format enum is ordered by size */
530 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
531 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
533 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
535 max_fs
= pa_frame_size(&max_ss
);
536 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
538 if (r
->remap_buf_contains_leftover_data
)
539 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
541 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
544 void pa_resampler_reset(pa_resampler
*r
) {
550 r
->remap_buf_contains_leftover_data
= false;
553 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
559 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
565 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
571 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
577 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
583 static const char * const resample_methods
[] = {
584 "src-sinc-best-quality",
585 "src-sinc-medium-quality",
587 "src-zero-order-hold",
618 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
620 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
623 return resample_methods
[m
];
626 int pa_resample_method_supported(pa_resample_method_t m
) {
628 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
631 #ifndef HAVE_LIBSAMPLERATE
632 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
637 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
639 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
646 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
647 pa_resample_method_t m
;
651 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
652 if (pa_streq(string
, resample_methods
[m
]))
655 if (pa_streq(string
, "speex-fixed"))
656 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
658 if (pa_streq(string
, "speex-float"))
659 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
661 return PA_RESAMPLER_INVALID
;
664 static bool on_left(pa_channel_position_t p
) {
667 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
668 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
669 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
670 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
671 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
672 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
675 static bool on_right(pa_channel_position_t p
) {
678 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
679 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
680 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
681 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
682 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
683 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
686 static bool on_center(pa_channel_position_t p
) {
689 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
690 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
691 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
692 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
693 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
696 static bool on_lfe(pa_channel_position_t p
) {
698 p
== PA_CHANNEL_POSITION_LFE
;
701 static bool on_front(pa_channel_position_t p
) {
703 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
704 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
705 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
706 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
707 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
708 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
709 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
710 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
713 static bool on_rear(pa_channel_position_t p
) {
715 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
716 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
717 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
718 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
719 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
720 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
723 static bool on_side(pa_channel_position_t p
) {
725 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
726 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
727 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
737 static int front_rear_side(pa_channel_position_t p
) {
747 static void calc_map_table(pa_resampler
*r
) {
750 bool ic_connected
[PA_CHANNELS_MAX
];
758 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
)))))
763 n_oc
= r
->o_ss
.channels
;
764 n_ic
= r
->i_ss
.channels
;
766 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
767 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
769 memset(ic_connected
, 0, sizeof(ic_connected
));
770 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
772 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
775 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
776 m
->map_table_f
[oc
][oc
] = 1.0f
;
778 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
780 for (oc
= 0; oc
< n_oc
; oc
++) {
781 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
783 for (ic
= 0; ic
< n_ic
; ic
++) {
784 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
786 /* We shall not do any remixing. Hence, just check by name */
788 m
->map_table_f
[oc
][ic
] = 1.0f
;
793 /* OK, we shall do the full monty: upmixing and downmixing. Our
794 * algorithm is relatively simple, does not do spacialization, delay
795 * elements or apply lowpass filters for LFE. Patches are always
796 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
797 * probably wouldn't make any sense anyway.)
799 * This code is not idempotent: downmixing an upmixed stereo stream is
800 * not identical to the original. The volume will not match, and the
801 * two channels will be a linear combination of both.
803 * This is loosely based on random suggestions found on the Internet,
805 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
808 * The algorithm works basically like this:
810 * 1) Connect all channels with matching names.
813 * S:Mono: Copy into all D:channels
814 * D:Mono: Avg all S:channels
816 * 3) Mix D:Left, D:Right:
817 * D:Left: If not connected, avg all S:Left
818 * D:Right: If not connected, avg all S:Right
821 * If not connected, avg all S:Center
822 * If still not connected, avg all S:Left, S:Right
825 * If not connected, avg all S:*
827 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
828 * connected, mix into all D:left and all D:right channels. Gain is
831 * 7) Make sure S:Center, S:LFE is used:
833 * S:Center, S:LFE: If not connected, mix into all D:left, all
834 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
835 * for LFE. C-front is only mixed into L-front/R-front if available,
836 * otherwise into all L/R channels. Similarly for C-rear.
838 * 8) Normalize each row in the matrix such that the sum for each row is
839 * not larger than 1.0 in order to avoid clipping.
841 * S: and D: shall relate to the source resp. destination channels.
843 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
844 * rear if needed. For 4: we try to find some suitable C source for C,
845 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
846 * channels. For 6: the rear channels should not be dropped entirely,
847 * however have only minimal impact. For 7: movies usually encode
848 * speech on the center channel. Thus we have to make sure this channel
849 * is distributed to L and R if not available in the output. Also, LFE
850 * is used to achieve a greater dynamic range, and thus we should try
851 * to do our best to pass it to L+R.
858 ic_unconnected_left
= 0,
859 ic_unconnected_right
= 0,
860 ic_unconnected_center
= 0,
861 ic_unconnected_lfe
= 0;
862 bool ic_unconnected_center_mixed_in
= 0;
866 for (ic
= 0; ic
< n_ic
; ic
++) {
867 if (on_left(r
->i_cm
.map
[ic
]))
869 if (on_right(r
->i_cm
.map
[ic
]))
871 if (on_center(r
->i_cm
.map
[ic
]))
875 for (oc
= 0; oc
< n_oc
; oc
++) {
876 bool oc_connected
= false;
877 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
879 for (ic
= 0; ic
< n_ic
; ic
++) {
880 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
882 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
883 m
->map_table_f
[oc
][ic
] = 1.0f
;
886 ic_connected
[ic
] = true;
888 else if (b
== PA_CHANNEL_POSITION_MONO
) {
889 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
892 ic_connected
[ic
] = true;
897 /* Try to find matching input ports for this output port */
901 /* We are not connected and on the left side, let's
902 * average all left side input channels. */
905 for (ic
= 0; ic
< n_ic
; ic
++)
906 if (on_left(r
->i_cm
.map
[ic
])) {
907 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
908 ic_connected
[ic
] = true;
911 /* We ignore the case where there is no left input channel.
912 * Something is really wrong in this case anyway. */
914 } else if (on_right(b
)) {
916 /* We are not connected and on the right side, let's
917 * average all right side input channels. */
920 for (ic
= 0; ic
< n_ic
; ic
++)
921 if (on_right(r
->i_cm
.map
[ic
])) {
922 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
923 ic_connected
[ic
] = true;
926 /* We ignore the case where there is no right input
927 * channel. Something is really wrong in this case anyway.
930 } else if (on_center(b
)) {
934 /* We are not connected and at the center. Let's average
935 * all center input channels. */
937 for (ic
= 0; ic
< n_ic
; ic
++)
938 if (on_center(r
->i_cm
.map
[ic
])) {
939 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
940 ic_connected
[ic
] = true;
943 } else if (ic_left
+ ic_right
> 0) {
945 /* Hmm, no center channel around, let's synthesize it
946 * by mixing L and R.*/
948 for (ic
= 0; ic
< n_ic
; ic
++)
949 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
950 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
951 ic_connected
[ic
] = true;
955 /* We ignore the case where there is not even a left or
956 * right input channel. Something is really wrong in this
959 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
961 /* We are not connected and an LFE. Let's average all
962 * channels for LFE. */
964 for (ic
= 0; ic
< n_ic
; ic
++)
965 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
967 /* Please note that a channel connected to LFE doesn't
968 * really count as connected. */
973 for (ic
= 0; ic
< n_ic
; ic
++) {
974 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
976 if (ic_connected
[ic
])
980 ic_unconnected_left
++;
981 else if (on_right(a
))
982 ic_unconnected_right
++;
983 else if (on_center(a
))
984 ic_unconnected_center
++;
986 ic_unconnected_lfe
++;
989 for (ic
= 0; ic
< n_ic
; ic
++) {
990 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
992 if (ic_connected
[ic
])
995 for (oc
= 0; oc
< n_oc
; oc
++) {
996 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
998 if (on_left(a
) && on_left(b
))
999 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1001 else if (on_right(a
) && on_right(b
))
1002 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1004 else if (on_center(a
) && on_center(b
)) {
1005 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1006 ic_unconnected_center_mixed_in
= true;
1008 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1009 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1013 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1014 unsigned ncenter
[PA_CHANNELS_MAX
];
1015 bool found_frs
[PA_CHANNELS_MAX
];
1017 memset(ncenter
, 0, sizeof(ncenter
));
1018 memset(found_frs
, 0, sizeof(found_frs
));
1020 /* Hmm, as it appears there was no center channel we
1021 could mix our center channel in. In this case, mix it into
1022 left and right. Using .5 as the factor. */
1024 for (ic
= 0; ic
< n_ic
; ic
++) {
1026 if (ic_connected
[ic
])
1029 if (!on_center(r
->i_cm
.map
[ic
]))
1032 for (oc
= 0; oc
< n_oc
; oc
++) {
1034 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1037 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1038 found_frs
[ic
] = true;
1043 for (oc
= 0; oc
< n_oc
; oc
++) {
1045 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1048 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1053 for (oc
= 0; oc
< n_oc
; oc
++) {
1055 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1058 if (ncenter
[oc
] <= 0)
1061 for (ic
= 0; ic
< n_ic
; ic
++) {
1063 if (!on_center(r
->i_cm
.map
[ic
]))
1066 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1067 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1073 for (oc
= 0; oc
< n_oc
; oc
++) {
1075 for (ic
= 0; ic
< n_ic
; ic
++)
1076 sum
+= m
->map_table_f
[oc
][ic
];
1079 for (ic
= 0; ic
< n_ic
; ic
++)
1080 m
->map_table_f
[oc
][ic
] /= sum
;
1083 /* make an 16:16 int version of the matrix */
1084 for (oc
= 0; oc
< n_oc
; oc
++)
1085 for (ic
= 0; ic
< n_ic
; ic
++)
1086 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1088 s
= pa_strbuf_new();
1090 pa_strbuf_printf(s
, " ");
1091 for (ic
= 0; ic
< n_ic
; ic
++)
1092 pa_strbuf_printf(s
, " I%02u ", ic
);
1093 pa_strbuf_puts(s
, "\n +");
1095 for (ic
= 0; ic
< n_ic
; ic
++)
1096 pa_strbuf_printf(s
, "------");
1097 pa_strbuf_puts(s
, "\n");
1099 for (oc
= 0; oc
< n_oc
; oc
++) {
1100 pa_strbuf_printf(s
, "O%02u |", oc
);
1102 for (ic
= 0; ic
< n_ic
; ic
++)
1103 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1105 pa_strbuf_puts(s
, "\n");
1108 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1111 /* initialize the remapping function */
1115 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1121 pa_assert(input
->memblock
);
1123 /* Convert the incoming sample into the work sample format and place them
1124 * in to_work_format_buf. */
1126 if (!r
->to_work_format_func
|| !input
->length
)
1129 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1131 r
->to_work_format_buf
.index
= 0;
1132 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1134 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1135 if (r
->to_work_format_buf
.memblock
)
1136 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1138 r
->to_work_format_buf_samples
= n_samples
;
1139 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1142 src
= pa_memblock_acquire_chunk(input
);
1143 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1145 r
->to_work_format_func(n_samples
, src
, dst
);
1147 pa_memblock_release(input
->memblock
);
1148 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1150 return &r
->to_work_format_buf
;
1153 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1154 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1156 size_t leftover_length
= 0;
1161 pa_assert(input
->memblock
);
1163 /* Remap channels and place the result in remap_buf. There may be leftover
1164 * data in the beginning of remap_buf. The leftover data is already
1165 * remapped, so it's not part of the input, it's part of the output. */
1167 have_leftover
= r
->remap_buf_contains_leftover_data
;
1168 r
->remap_buf_contains_leftover_data
= false;
1170 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1172 else if (input
->length
<= 0)
1173 return &r
->remap_buf
;
1175 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1176 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1178 if (have_leftover
) {
1179 leftover_length
= r
->remap_buf
.length
;
1180 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1183 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1184 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1186 if (have_leftover
) {
1187 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1188 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1190 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1191 dst
= pa_memblock_acquire(new_block
);
1192 memcpy(dst
, src
, leftover_length
);
1193 pa_memblock_release(r
->remap_buf
.memblock
);
1194 pa_memblock_release(new_block
);
1196 pa_memblock_unref(r
->remap_buf
.memblock
);
1197 r
->remap_buf
.memblock
= new_block
;
1198 r
->remap_buf_size
= r
->remap_buf
.length
;
1202 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1203 if (r
->remap_buf
.memblock
)
1204 pa_memblock_unref(r
->remap_buf
.memblock
);
1206 r
->remap_buf_size
= r
->remap_buf
.length
;
1207 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1211 src
= pa_memblock_acquire_chunk(input
);
1212 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1214 if (r
->map_required
) {
1215 pa_remap_t
*remap
= &r
->remap
;
1217 pa_assert(remap
->do_remap
);
1218 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1221 memcpy(dst
, src
, input
->length
);
1223 pa_memblock_release(input
->memblock
);
1224 pa_memblock_release(r
->remap_buf
.memblock
);
1226 return &r
->remap_buf
;
1229 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1230 unsigned in_n_frames
, in_n_samples
;
1231 unsigned out_n_frames
, out_n_samples
;
1236 /* Resample the data and place the result in resample_buf. */
1238 if (!r
->impl
.resample
|| !input
->length
)
1241 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1242 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1244 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1245 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1247 r
->resample_buf
.index
= 0;
1248 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1250 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1251 if (r
->resample_buf
.memblock
)
1252 pa_memblock_unref(r
->resample_buf
.memblock
);
1254 r
->resample_buf_samples
= out_n_samples
;
1255 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1258 r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1259 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1261 return &r
->resample_buf
;
1264 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1265 unsigned n_samples
, n_frames
;
1271 /* Convert the data into the correct sample type and place the result in
1272 * from_work_format_buf. */
1274 if (!r
->from_work_format_func
|| !input
->length
)
1277 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1278 n_frames
= n_samples
/ r
->o_ss
.channels
;
1280 r
->from_work_format_buf
.index
= 0;
1281 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1283 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1284 if (r
->from_work_format_buf
.memblock
)
1285 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1287 r
->from_work_format_buf_samples
= n_samples
;
1288 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1291 src
= pa_memblock_acquire_chunk(input
);
1292 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1293 r
->from_work_format_func(n_samples
, src
, dst
);
1294 pa_memblock_release(input
->memblock
);
1295 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1297 return &r
->from_work_format_buf
;
1300 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1306 pa_assert(in
->length
);
1307 pa_assert(in
->memblock
);
1308 pa_assert(in
->length
% r
->i_fz
== 0);
1310 buf
= (pa_memchunk
*) in
;
1311 buf
= convert_to_work_format(r
, buf
);
1312 buf
= remap_channels(r
, buf
);
1313 buf
= resample(r
, buf
);
1316 buf
= convert_from_work_format(r
, buf
);
1320 pa_memblock_ref(buf
->memblock
);
1322 pa_memchunk_reset(buf
);
1324 pa_memchunk_reset(out
);
1327 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1334 /* Store the leftover to remap_buf. */
1336 r
->remap_buf
.length
= len
;
1338 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1339 if (r
->remap_buf
.memblock
)
1340 pa_memblock_unref(r
->remap_buf
.memblock
);
1342 r
->remap_buf_size
= r
->remap_buf
.length
;
1343 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1346 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1347 memcpy(dst
, buf
, r
->remap_buf
.length
);
1348 pa_memblock_release(r
->remap_buf
.memblock
);
1350 r
->remap_buf_contains_leftover_data
= true;
1353 /*** libsamplerate based implementation ***/
1355 #ifdef HAVE_LIBSAMPLERATE
1356 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1358 struct src_data
*libsamplerate_data
;
1363 pa_assert(out_n_frames
);
1365 libsamplerate_data
= r
->impl
.data
;
1366 memset(&data
, 0, sizeof(data
));
1368 data
.data_in
= pa_memblock_acquire_chunk(input
);
1369 data
.input_frames
= (long int) in_n_frames
;
1371 data
.data_out
= pa_memblock_acquire_chunk(output
);
1372 data
.output_frames
= (long int) *out_n_frames
;
1374 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1375 data
.end_of_input
= 0;
1377 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1379 if (data
.input_frames_used
< in_n_frames
) {
1380 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1381 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1383 save_leftover(r
, leftover_data
, leftover_length
);
1386 pa_memblock_release(input
->memblock
);
1387 pa_memblock_release(output
->memblock
);
1389 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1392 static void libsamplerate_update_rates(pa_resampler
*r
) {
1393 struct src_data
*libsamplerate_data
;
1396 libsamplerate_data
= r
->impl
.data
;
1397 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1400 static void libsamplerate_reset(pa_resampler
*r
) {
1401 struct src_data
*libsamplerate_data
;
1404 libsamplerate_data
= r
->impl
.data
;
1405 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1408 static void libsamplerate_free(pa_resampler
*r
) {
1409 struct src_data
*libsamplerate_data
;
1412 libsamplerate_data
= r
->impl
.data
;
1413 if (libsamplerate_data
->state
)
1414 src_delete(libsamplerate_data
->state
);
1417 static int libsamplerate_init(pa_resampler
*r
) {
1419 struct src_data
*libsamplerate_data
;
1423 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1425 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1428 r
->impl
.free
= libsamplerate_free
;
1429 r
->impl
.update_rates
= libsamplerate_update_rates
;
1430 r
->impl
.resample
= libsamplerate_resample
;
1431 r
->impl
.reset
= libsamplerate_reset
;
1432 r
->impl
.data
= libsamplerate_data
;
1439 /*** speex based implementation ***/
1441 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1443 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1444 struct speex_data
*speex_data
;
1449 pa_assert(out_n_frames
);
1451 speex_data
= r
->impl
.data
;
1453 in
= pa_memblock_acquire_chunk(input
);
1454 out
= pa_memblock_acquire_chunk(output
);
1456 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1458 pa_memblock_release(input
->memblock
);
1459 pa_memblock_release(output
->memblock
);
1461 pa_assert(inf
== in_n_frames
);
1462 *out_n_frames
= outf
;
1465 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1467 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1468 struct speex_data
*speex_data
;
1473 pa_assert(out_n_frames
);
1475 speex_data
= r
->impl
.data
;
1477 in
= pa_memblock_acquire_chunk(input
);
1478 out
= pa_memblock_acquire_chunk(output
);
1480 pa_assert_se(speex_resampler_process_interleaved_int(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1482 pa_memblock_release(input
->memblock
);
1483 pa_memblock_release(output
->memblock
);
1485 pa_assert(inf
== in_n_frames
);
1486 *out_n_frames
= outf
;
1489 static void speex_update_rates(pa_resampler
*r
) {
1490 struct speex_data
*speex_data
;
1493 speex_data
= r
->impl
.data
;
1495 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1498 static void speex_reset(pa_resampler
*r
) {
1499 struct speex_data
*speex_data
;
1502 speex_data
= r
->impl
.data
;
1504 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1507 static void speex_free(pa_resampler
*r
) {
1508 struct speex_data
*speex_data
;
1511 speex_data
= r
->impl
.data
;
1512 if (!speex_data
->state
)
1515 speex_resampler_destroy(speex_data
->state
);
1518 static int speex_init(pa_resampler
*r
) {
1520 struct speex_data
*speex_data
;
1524 speex_data
= pa_xnew(struct speex_data
, 1);
1526 r
->impl
.free
= speex_free
;
1527 r
->impl
.update_rates
= speex_update_rates
;
1528 r
->impl
.reset
= speex_reset
;
1529 r
->impl
.data
= speex_data
;
1531 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1533 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1534 r
->impl
.resample
= speex_resample_int
;
1537 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1539 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1540 r
->impl
.resample
= speex_resample_float
;
1543 pa_log_info("Choosing speex quality setting %i.", q
);
1545 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1552 /* Trivial implementation */
1554 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1556 unsigned i_index
, o_index
;
1558 struct trivial_data
*trivial_data
;
1563 pa_assert(out_n_frames
);
1565 trivial_data
= r
->impl
.data
;
1566 fz
= r
->w_sz
* r
->o_ss
.channels
;
1568 src
= pa_memblock_acquire_chunk(input
);
1569 dst
= pa_memblock_acquire_chunk(output
);
1571 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1572 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1573 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1575 if (i_index
>= in_n_frames
)
1578 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1580 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1583 pa_memblock_release(input
->memblock
);
1584 pa_memblock_release(output
->memblock
);
1586 *out_n_frames
= o_index
;
1588 trivial_data
->i_counter
+= in_n_frames
;
1590 /* Normalize counters */
1591 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1592 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1594 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1595 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1599 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1600 struct trivial_data
*trivial_data
;
1603 trivial_data
= r
->impl
.data
;
1605 trivial_data
->i_counter
= 0;
1606 trivial_data
->o_counter
= 0;
1609 static int trivial_init(pa_resampler
*r
) {
1610 struct trivial_data
*trivial_data
;
1613 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1615 r
->impl
.resample
= trivial_resample
;
1616 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1617 r
->impl
.reset
= trivial_update_rates_or_reset
;
1618 r
->impl
.data
= trivial_data
;
1623 /* Peak finder implementation */
1625 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1626 unsigned c
, o_index
= 0;
1627 unsigned i
, i_end
= 0;
1629 struct peaks_data
*peaks_data
;
1634 pa_assert(out_n_frames
);
1636 peaks_data
= r
->impl
.data
;
1637 src
= pa_memblock_acquire_chunk(input
);
1638 dst
= pa_memblock_acquire_chunk(output
);
1640 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1641 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1643 while (i_end
< in_n_frames
) {
1644 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1645 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1647 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1649 /* 1ch float is treated separately, because that is the common case */
1650 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1651 float *s
= (float*) src
+ i
;
1652 float *d
= (float*) dst
+ o_index
;
1654 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1655 float n
= fabsf(*s
++);
1657 if (n
> peaks_data
->max_f
[0])
1658 peaks_data
->max_f
[0] = n
;
1662 *d
= peaks_data
->max_f
[0];
1663 peaks_data
->max_f
[0] = 0;
1664 o_index
++, peaks_data
->o_counter
++;
1666 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1667 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1668 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1670 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1671 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1672 int16_t n
= abs(*s
++);
1674 if (n
> peaks_data
->max_i
[c
])
1675 peaks_data
->max_i
[c
] = n
;
1679 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1680 *d
= peaks_data
->max_i
[c
];
1681 peaks_data
->max_i
[c
] = 0;
1683 o_index
++, peaks_data
->o_counter
++;
1686 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1687 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1689 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1690 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1691 float n
= fabsf(*s
++);
1693 if (n
> peaks_data
->max_f
[c
])
1694 peaks_data
->max_f
[c
] = n
;
1698 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1699 *d
= peaks_data
->max_f
[c
];
1700 peaks_data
->max_f
[c
] = 0;
1702 o_index
++, peaks_data
->o_counter
++;
1707 pa_memblock_release(input
->memblock
);
1708 pa_memblock_release(output
->memblock
);
1710 *out_n_frames
= o_index
;
1712 peaks_data
->i_counter
+= in_n_frames
;
1714 /* Normalize counters */
1715 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1716 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1718 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1719 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1723 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1724 struct peaks_data
*peaks_data
;
1727 peaks_data
= r
->impl
.data
;
1729 peaks_data
->i_counter
= 0;
1730 peaks_data
->o_counter
= 0;
1733 static int peaks_init(pa_resampler
*r
) {
1734 struct peaks_data
*peaks_data
;
1736 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1737 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1739 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1741 r
->impl
.resample
= peaks_resample
;
1742 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1743 r
->impl
.reset
= peaks_update_rates_or_reset
;
1744 r
->impl
.data
= peaks_data
;
1749 /*** ffmpeg based implementation ***/
1751 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1752 unsigned used_frames
= 0, c
;
1753 int previous_consumed_frames
= -1;
1754 struct ffmpeg_data
*ffmpeg_data
;
1759 pa_assert(out_n_frames
);
1761 ffmpeg_data
= r
->impl
.data
;
1763 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1766 int16_t *p
, *t
, *k
, *q
, *s
;
1767 int consumed_frames
;
1769 /* Allocate a new block */
1770 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1771 p
= pa_memblock_acquire(b
);
1773 /* Now copy the input data, splitting up channels */
1774 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1776 for (u
= 0; u
< in_n_frames
; u
++) {
1778 t
+= r
->o_ss
.channels
;
1781 pa_memblock_release(input
->memblock
);
1783 /* Allocate buffer for the result */
1784 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1785 q
= pa_memblock_acquire(w
);
1788 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1791 (int) in_n_frames
, (int) *out_n_frames
,
1792 c
>= (unsigned) (r
->o_ss
.channels
-1));
1794 pa_memblock_release(b
);
1795 pa_memblock_unref(b
);
1797 pa_assert(consumed_frames
<= (int) in_n_frames
);
1798 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1799 previous_consumed_frames
= consumed_frames
;
1801 /* And place the results in the output buffer */
1802 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1803 for (u
= 0; u
< used_frames
; u
++) {
1806 s
+= r
->o_ss
.channels
;
1808 pa_memblock_release(output
->memblock
);
1809 pa_memblock_release(w
);
1810 pa_memblock_unref(w
);
1813 if (previous_consumed_frames
< (int) in_n_frames
) {
1814 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1815 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1817 save_leftover(r
, leftover_data
, leftover_length
);
1818 pa_memblock_release(input
->memblock
);
1821 *out_n_frames
= used_frames
;
1824 static void ffmpeg_free(pa_resampler
*r
) {
1826 struct ffmpeg_data
*ffmpeg_data
;
1830 ffmpeg_data
= r
->impl
.data
;
1831 if (ffmpeg_data
->state
)
1832 av_resample_close(ffmpeg_data
->state
);
1834 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1835 if (ffmpeg_data
->buf
[c
].memblock
)
1836 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1839 static int ffmpeg_init(pa_resampler
*r
) {
1841 struct ffmpeg_data
*ffmpeg_data
;
1845 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1847 /* We could probably implement different quality levels by
1848 * adjusting the filter parameters here. However, ffmpeg
1849 * internally only uses these hardcoded values, so let's use them
1850 * here for now as well until ffmpeg makes this configurable. */
1852 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1855 r
->impl
.free
= ffmpeg_free
;
1856 r
->impl
.resample
= ffmpeg_resample
;
1857 r
->impl
.data
= (void *) ffmpeg_data
;
1859 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1860 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1865 /*** copy (noop) implementation ***/
1867 static int copy_init(pa_resampler
*r
) {
1870 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);