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_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 size_t to_work_format_buf_size
;
64 size_t remap_buf_size
;
65 size_t resample_buf_size
;
66 size_t from_work_format_buf_size
;
68 /* points to buffer before resampling stage, remap or to_work */
69 pa_memchunk
*leftover_buf
;
70 size_t *leftover_buf_size
;
72 /* have_leftover points to leftover_in_remap or leftover_in_to_work */
74 bool leftover_in_remap
;
75 bool leftover_in_to_work
;
77 pa_sample_format_t work_format
;
78 uint8_t work_channels
;
80 pa_convert_func_t to_work_format_func
;
81 pa_convert_func_t from_work_format_func
;
86 pa_resampler_impl impl
;
89 struct trivial_data
{ /* data specific to the trivial resampler */
94 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
98 float max_f
[PA_CHANNELS_MAX
];
99 int16_t max_i
[PA_CHANNELS_MAX
];
102 struct ffmpeg_data
{ /* data specific to ffmpeg */
103 struct AVResampleContext
*state
;
106 static int copy_init(pa_resampler
*r
);
107 static int trivial_init(pa_resampler
*r
);
109 static int speex_init(pa_resampler
*r
);
111 static int ffmpeg_init(pa_resampler
*r
);
112 static int peaks_init(pa_resampler
*r
);
113 #ifdef HAVE_LIBSAMPLERATE
114 static int libsamplerate_init(pa_resampler
*r
);
117 static void calc_map_table(pa_resampler
*r
);
119 static int (* const init_table
[])(pa_resampler
*r
) = {
120 #ifdef HAVE_LIBSAMPLERATE
121 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
122 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
123 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
124 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
125 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
127 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
128 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
129 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
130 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
131 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
133 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
135 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
136 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
137 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
138 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
139 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
140 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
141 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
142 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
159 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
160 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
161 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
162 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
163 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
164 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
165 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
169 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
170 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
171 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
172 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
173 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
174 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
175 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
176 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
181 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
182 [PA_RESAMPLER_AUTO
] = NULL
,
183 [PA_RESAMPLER_COPY
] = copy_init
,
184 [PA_RESAMPLER_PEAKS
] = peaks_init
,
187 static pa_resample_method_t
choose_auto_resampler(pa_resample_flags_t flags
) {
188 pa_resample_method_t method
;
190 if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1))
191 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
192 else if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
193 method
= PA_RESAMPLER_TRIVIAL
;
195 method
= PA_RESAMPLER_FFMPEG
;
200 static pa_resample_method_t
pa_resampler_fix_method(
201 pa_resample_flags_t flags
,
202 pa_resample_method_t method
,
203 const uint32_t rate_a
,
204 const uint32_t rate_b
) {
206 pa_assert(pa_sample_rate_valid(rate_a
));
207 pa_assert(pa_sample_rate_valid(rate_b
));
208 pa_assert(method
>= 0);
209 pa_assert(method
< PA_RESAMPLER_MAX
);
211 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
212 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
213 method
= PA_RESAMPLER_COPY
;
216 if (!pa_resample_method_supported(method
)) {
217 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
218 method
= PA_RESAMPLER_AUTO
;
222 case PA_RESAMPLER_COPY
:
223 if (rate_a
!= rate_b
) {
224 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
225 method
= PA_RESAMPLER_AUTO
;
228 /* Else fall through */
229 case PA_RESAMPLER_FFMPEG
:
230 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
231 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
232 method
= PA_RESAMPLER_AUTO
;
236 /* The Peaks resampler only supports downsampling.
237 * Revert to auto if we are upsampling */
238 case PA_RESAMPLER_PEAKS
:
239 if (rate_a
< rate_b
) {
240 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
241 method
= PA_RESAMPLER_AUTO
;
249 if (method
== PA_RESAMPLER_AUTO
)
250 method
= choose_auto_resampler(flags
);
255 /* Return true if a is a more precise sample format than b, else return false */
256 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
257 pa_assert(pa_sample_format_valid(a
));
258 pa_assert(pa_sample_format_valid(b
));
267 case PA_SAMPLE_S16LE
:
268 case PA_SAMPLE_S16BE
:
269 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
275 case PA_SAMPLE_S24LE
:
276 case PA_SAMPLE_S24BE
:
277 case PA_SAMPLE_S24_32LE
:
278 case PA_SAMPLE_S24_32BE
:
279 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
280 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
286 case PA_SAMPLE_FLOAT32LE
:
287 case PA_SAMPLE_FLOAT32BE
:
288 case PA_SAMPLE_S32LE
:
289 case PA_SAMPLE_S32BE
:
290 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
291 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
302 static pa_sample_format_t
pa_resampler_choose_work_format(
303 pa_resample_method_t method
,
304 pa_sample_format_t a
,
305 pa_sample_format_t b
,
307 pa_sample_format_t work_format
;
309 pa_assert(pa_sample_format_valid(a
));
310 pa_assert(pa_sample_format_valid(b
));
311 pa_assert(method
>= 0);
312 pa_assert(method
< PA_RESAMPLER_MAX
);
314 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
315 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
318 /* This block is for resampling functions that only
319 * support the S16 sample format. */
320 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
321 case PA_RESAMPLER_FFMPEG
:
322 work_format
= PA_SAMPLE_S16NE
;
325 /* This block is for resampling functions that support
326 * any sample format. */
327 case PA_RESAMPLER_COPY
: /* fall through */
328 case PA_RESAMPLER_TRIVIAL
:
329 if (!map_required
&& a
== b
) {
333 /* Else fall trough */
334 case PA_RESAMPLER_PEAKS
:
335 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
336 work_format
= PA_SAMPLE_S16NE
;
337 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
338 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
339 work_format
= PA_SAMPLE_FLOAT32NE
;
341 work_format
= PA_SAMPLE_S16NE
;
345 work_format
= PA_SAMPLE_FLOAT32NE
;
351 pa_resampler
* pa_resampler_new(
353 const pa_sample_spec
*a
,
354 const pa_channel_map
*am
,
355 const pa_sample_spec
*b
,
356 const pa_channel_map
*bm
,
357 pa_resample_method_t method
,
358 pa_resample_flags_t flags
) {
360 pa_resampler
*r
= NULL
;
365 pa_assert(pa_sample_spec_valid(a
));
366 pa_assert(pa_sample_spec_valid(b
));
367 pa_assert(method
>= 0);
368 pa_assert(method
< PA_RESAMPLER_MAX
);
370 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
372 r
= pa_xnew0(pa_resampler
, 1);
377 /* Fill sample specs */
381 /* set up the remap structure */
382 r
->remap
.i_ss
= &r
->i_ss
;
383 r
->remap
.o_ss
= &r
->o_ss
;
384 r
->remap
.format
= &r
->work_format
;
388 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
393 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
396 r
->i_fz
= pa_frame_size(a
);
397 r
->o_fz
= pa_frame_size(b
);
401 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
402 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
404 if (r
->i_ss
.format
!= r
->work_format
) {
405 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
406 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
409 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
410 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
415 if (r
->o_ss
.format
!= r
->work_format
) {
416 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
417 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
420 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
421 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
426 if (r
->o_ss
.channels
<= r
->i_ss
.channels
) {
427 /* pipeline is: format conv. -> remap -> resample -> format conv. */
428 r
->work_channels
= r
->o_ss
.channels
;
430 /* leftover buffer is remap output buffer (before resampling) */
431 r
->leftover_buf
= &r
->remap_buf
;
432 r
->leftover_buf_size
= &r
->remap_buf_size
;
433 r
->have_leftover
= &r
->leftover_in_remap
;
435 /* pipeline is: format conv. -> resample -> remap -> format conv. */
436 r
->work_channels
= r
->i_ss
.channels
;
438 /* leftover buffer is to_work output buffer (before resampling) */
439 r
->leftover_buf
= &r
->to_work_format_buf
;
440 r
->leftover_buf_size
= &r
->to_work_format_buf_size
;
441 r
->have_leftover
= &r
->leftover_in_to_work
;
443 r
->w_fz
= pa_sample_size_of_format(r
->work_format
) * r
->work_channels
;
445 pa_log_debug("Resampler:");
446 pa_log_debug(" rate %d -> %d (method %s)", a
->rate
, b
->rate
, pa_resample_method_to_string(r
->method
));
447 pa_log_debug(" format %s -> %s (intermediate %s)", pa_sample_format_to_string(a
->format
),
448 pa_sample_format_to_string(b
->format
), pa_sample_format_to_string(r
->work_format
));
449 pa_log_debug(" channels %d -> %d (resampling %d)", a
->channels
, b
->channels
, r
->work_channels
);
451 /* initialize implementation */
452 if (init_table
[method
](r
) < 0)
463 void pa_resampler_free(pa_resampler
*r
) {
469 pa_xfree(r
->impl
.data
);
471 if (r
->to_work_format_buf
.memblock
)
472 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
473 if (r
->remap_buf
.memblock
)
474 pa_memblock_unref(r
->remap_buf
.memblock
);
475 if (r
->resample_buf
.memblock
)
476 pa_memblock_unref(r
->resample_buf
.memblock
);
477 if (r
->from_work_format_buf
.memblock
)
478 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
483 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
486 pa_assert(r
->impl
.update_rates
);
488 if (r
->i_ss
.rate
== rate
)
493 r
->impl
.update_rates(r
);
496 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
499 pa_assert(r
->impl
.update_rates
);
501 if (r
->o_ss
.rate
== rate
)
506 r
->impl
.update_rates(r
);
509 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
512 /* Let's round up here to make it more likely that the caller will get at
513 * least out_length amount of data from pa_resampler_run().
515 * We don't take the leftover into account here. If we did, then it might
516 * be in theory possible that this function would return 0 and
517 * pa_resampler_run() would also return 0. That could lead to infinite
518 * loops. When the leftover is ignored here, such loops would eventually
519 * terminate, because the leftover would grow each round, finally
520 * surpassing the minimum input threshold of the resampler. */
521 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
;
524 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
529 /* Let's round up here to ensure that the caller will always allocate big
530 * enough output buffer. */
532 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
533 if (*r
->have_leftover
)
534 frames
+= r
->leftover_buf
->length
/ r
->w_fz
;
536 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
539 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
540 size_t block_size_max
;
541 pa_sample_spec max_ss
;
547 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
549 /* We deduce the "largest" sample spec we're using during the
551 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
553 /* We silently assume that the format enum is ordered by size */
554 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
555 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
557 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
559 max_fs
= pa_frame_size(&max_ss
);
560 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
562 pa_assert(frames
>= (r
->leftover_buf
->length
/ r
->w_fz
));
563 if (*r
->have_leftover
)
564 frames
-= r
->leftover_buf
->length
/ r
->w_fz
;
566 block_size_max
= ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
568 if (block_size_max
> 0)
569 return block_size_max
;
571 /* A single input frame may result in so much output that it doesn't
572 * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
573 * this case the max block size will be set to one frame, and some
574 * memory will be probably be allocated with malloc() instead of using
577 * XXX: Should we support this case at all? We could also refuse to
578 * create resamplers whose max block size would exceed the memory pool
579 * block size. In this case also updating the resampler rate should
580 * fail if the new rate would cause an excessive max block size (in
581 * which case the stream would probably have to be killed). */
585 void pa_resampler_reset(pa_resampler
*r
) {
591 *r
->have_leftover
= false;
594 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
600 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
606 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
612 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
618 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
624 static const char * const resample_methods
[] = {
625 "src-sinc-best-quality",
626 "src-sinc-medium-quality",
628 "src-zero-order-hold",
659 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
661 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
664 return resample_methods
[m
];
667 int pa_resample_method_supported(pa_resample_method_t m
) {
669 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
672 #ifndef HAVE_LIBSAMPLERATE
673 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
678 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
680 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
687 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
688 pa_resample_method_t m
;
692 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
693 if (pa_streq(string
, resample_methods
[m
]))
696 if (pa_streq(string
, "speex-fixed"))
697 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
699 if (pa_streq(string
, "speex-float"))
700 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
702 return PA_RESAMPLER_INVALID
;
705 static bool on_left(pa_channel_position_t p
) {
708 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
709 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
710 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
711 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
712 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
713 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
716 static bool on_right(pa_channel_position_t p
) {
719 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
720 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
721 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
722 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
723 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
724 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
727 static bool on_center(pa_channel_position_t p
) {
730 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
731 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
732 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
733 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
734 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
737 static bool on_lfe(pa_channel_position_t p
) {
739 p
== PA_CHANNEL_POSITION_LFE
;
742 static bool on_front(pa_channel_position_t p
) {
744 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
745 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
746 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
747 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
748 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
749 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
750 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
751 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
754 static bool on_rear(pa_channel_position_t p
) {
756 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
757 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
758 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
759 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
760 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
761 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
764 static bool on_side(pa_channel_position_t p
) {
766 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
767 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
768 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
778 static int front_rear_side(pa_channel_position_t p
) {
788 static void calc_map_table(pa_resampler
*r
) {
791 bool ic_connected
[PA_CHANNELS_MAX
];
799 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
)))))
804 n_oc
= r
->o_ss
.channels
;
805 n_ic
= r
->i_ss
.channels
;
807 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
808 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
810 memset(ic_connected
, 0, sizeof(ic_connected
));
811 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
813 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
816 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
817 m
->map_table_f
[oc
][oc
] = 1.0f
;
819 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
821 for (oc
= 0; oc
< n_oc
; oc
++) {
822 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
824 for (ic
= 0; ic
< n_ic
; ic
++) {
825 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
827 /* We shall not do any remixing. Hence, just check by name */
829 m
->map_table_f
[oc
][ic
] = 1.0f
;
834 /* OK, we shall do the full monty: upmixing and downmixing. Our
835 * algorithm is relatively simple, does not do spacialization, delay
836 * elements or apply lowpass filters for LFE. Patches are always
837 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
838 * probably wouldn't make any sense anyway.)
840 * This code is not idempotent: downmixing an upmixed stereo stream is
841 * not identical to the original. The volume will not match, and the
842 * two channels will be a linear combination of both.
844 * This is loosely based on random suggestions found on the Internet,
846 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
849 * The algorithm works basically like this:
851 * 1) Connect all channels with matching names.
854 * S:Mono: Copy into all D:channels
855 * D:Mono: Avg all S:channels
857 * 3) Mix D:Left, D:Right:
858 * D:Left: If not connected, avg all S:Left
859 * D:Right: If not connected, avg all S:Right
862 * If not connected, avg all S:Center
863 * If still not connected, avg all S:Left, S:Right
866 * If not connected, avg all S:*
868 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
869 * connected, mix into all D:left and all D:right channels. Gain is
872 * 7) Make sure S:Center, S:LFE is used:
874 * S:Center, S:LFE: If not connected, mix into all D:left, all
875 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
876 * for LFE. C-front is only mixed into L-front/R-front if available,
877 * otherwise into all L/R channels. Similarly for C-rear.
879 * 8) Normalize each row in the matrix such that the sum for each row is
880 * not larger than 1.0 in order to avoid clipping.
882 * S: and D: shall relate to the source resp. destination channels.
884 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
885 * rear if needed. For 4: we try to find some suitable C source for C,
886 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
887 * channels. For 6: the rear channels should not be dropped entirely,
888 * however have only minimal impact. For 7: movies usually encode
889 * speech on the center channel. Thus we have to make sure this channel
890 * is distributed to L and R if not available in the output. Also, LFE
891 * is used to achieve a greater dynamic range, and thus we should try
892 * to do our best to pass it to L+R.
899 ic_unconnected_left
= 0,
900 ic_unconnected_right
= 0,
901 ic_unconnected_center
= 0,
902 ic_unconnected_lfe
= 0;
903 bool ic_unconnected_center_mixed_in
= 0;
907 for (ic
= 0; ic
< n_ic
; ic
++) {
908 if (on_left(r
->i_cm
.map
[ic
]))
910 if (on_right(r
->i_cm
.map
[ic
]))
912 if (on_center(r
->i_cm
.map
[ic
]))
916 for (oc
= 0; oc
< n_oc
; oc
++) {
917 bool oc_connected
= false;
918 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
920 for (ic
= 0; ic
< n_ic
; ic
++) {
921 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
923 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
924 m
->map_table_f
[oc
][ic
] = 1.0f
;
927 ic_connected
[ic
] = true;
929 else if (b
== PA_CHANNEL_POSITION_MONO
) {
930 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
933 ic_connected
[ic
] = true;
938 /* Try to find matching input ports for this output port */
942 /* We are not connected and on the left side, let's
943 * average all left side input channels. */
946 for (ic
= 0; ic
< n_ic
; ic
++)
947 if (on_left(r
->i_cm
.map
[ic
])) {
948 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
949 ic_connected
[ic
] = true;
952 /* We ignore the case where there is no left input channel.
953 * Something is really wrong in this case anyway. */
955 } else if (on_right(b
)) {
957 /* We are not connected and on the right side, let's
958 * average all right side input channels. */
961 for (ic
= 0; ic
< n_ic
; ic
++)
962 if (on_right(r
->i_cm
.map
[ic
])) {
963 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
964 ic_connected
[ic
] = true;
967 /* We ignore the case where there is no right input
968 * channel. Something is really wrong in this case anyway.
971 } else if (on_center(b
)) {
975 /* We are not connected and at the center. Let's average
976 * all center input channels. */
978 for (ic
= 0; ic
< n_ic
; ic
++)
979 if (on_center(r
->i_cm
.map
[ic
])) {
980 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
981 ic_connected
[ic
] = true;
984 } else if (ic_left
+ ic_right
> 0) {
986 /* Hmm, no center channel around, let's synthesize it
987 * by mixing L and R.*/
989 for (ic
= 0; ic
< n_ic
; ic
++)
990 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
991 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
992 ic_connected
[ic
] = true;
996 /* We ignore the case where there is not even a left or
997 * right input channel. Something is really wrong in this
1000 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
1002 /* We are not connected and an LFE. Let's average all
1003 * channels for LFE. */
1005 for (ic
= 0; ic
< n_ic
; ic
++)
1006 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
1008 /* Please note that a channel connected to LFE doesn't
1009 * really count as connected. */
1014 for (ic
= 0; ic
< n_ic
; ic
++) {
1015 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
1017 if (ic_connected
[ic
])
1021 ic_unconnected_left
++;
1022 else if (on_right(a
))
1023 ic_unconnected_right
++;
1024 else if (on_center(a
))
1025 ic_unconnected_center
++;
1027 ic_unconnected_lfe
++;
1030 for (ic
= 0; ic
< n_ic
; ic
++) {
1031 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
1033 if (ic_connected
[ic
])
1036 for (oc
= 0; oc
< n_oc
; oc
++) {
1037 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
1039 if (on_left(a
) && on_left(b
))
1040 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1042 else if (on_right(a
) && on_right(b
))
1043 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1045 else if (on_center(a
) && on_center(b
)) {
1046 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1047 ic_unconnected_center_mixed_in
= true;
1049 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1050 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1054 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1055 unsigned ncenter
[PA_CHANNELS_MAX
];
1056 bool found_frs
[PA_CHANNELS_MAX
];
1058 memset(ncenter
, 0, sizeof(ncenter
));
1059 memset(found_frs
, 0, sizeof(found_frs
));
1061 /* Hmm, as it appears there was no center channel we
1062 could mix our center channel in. In this case, mix it into
1063 left and right. Using .5 as the factor. */
1065 for (ic
= 0; ic
< n_ic
; ic
++) {
1067 if (ic_connected
[ic
])
1070 if (!on_center(r
->i_cm
.map
[ic
]))
1073 for (oc
= 0; oc
< n_oc
; oc
++) {
1075 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1078 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1079 found_frs
[ic
] = true;
1084 for (oc
= 0; oc
< n_oc
; oc
++) {
1086 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1089 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1094 for (oc
= 0; oc
< n_oc
; oc
++) {
1096 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1099 if (ncenter
[oc
] <= 0)
1102 for (ic
= 0; ic
< n_ic
; ic
++) {
1104 if (!on_center(r
->i_cm
.map
[ic
]))
1107 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1108 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1114 for (oc
= 0; oc
< n_oc
; oc
++) {
1116 for (ic
= 0; ic
< n_ic
; ic
++)
1117 sum
+= m
->map_table_f
[oc
][ic
];
1120 for (ic
= 0; ic
< n_ic
; ic
++)
1121 m
->map_table_f
[oc
][ic
] /= sum
;
1124 /* make an 16:16 int version of the matrix */
1125 for (oc
= 0; oc
< n_oc
; oc
++)
1126 for (ic
= 0; ic
< n_ic
; ic
++)
1127 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1129 s
= pa_strbuf_new();
1131 pa_strbuf_printf(s
, " ");
1132 for (ic
= 0; ic
< n_ic
; ic
++)
1133 pa_strbuf_printf(s
, " I%02u ", ic
);
1134 pa_strbuf_puts(s
, "\n +");
1136 for (ic
= 0; ic
< n_ic
; ic
++)
1137 pa_strbuf_printf(s
, "------");
1138 pa_strbuf_puts(s
, "\n");
1140 for (oc
= 0; oc
< n_oc
; oc
++) {
1141 pa_strbuf_printf(s
, "O%02u |", oc
);
1143 for (ic
= 0; ic
< n_ic
; ic
++)
1144 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1146 pa_strbuf_puts(s
, "\n");
1149 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1152 /* initialize the remapping function */
1156 /* check if buf's memblock is large enough to hold 'len' bytes; create a
1157 * new memblock if necessary and optionally preserve 'copy' data bytes */
1158 static void fit_buf(pa_resampler
*r
, pa_memchunk
*buf
, size_t len
, size_t *size
, size_t copy
) {
1161 if (!buf
->memblock
|| len
> *size
) {
1162 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, len
);
1164 if (buf
->memblock
) {
1166 void *src
= pa_memblock_acquire(buf
->memblock
);
1167 void *dst
= pa_memblock_acquire(new_block
);
1168 pa_assert(copy
<= len
);
1169 memcpy(dst
, src
, copy
);
1170 pa_memblock_release(new_block
);
1171 pa_memblock_release(buf
->memblock
);
1174 pa_memblock_unref(buf
->memblock
);
1177 buf
->memblock
= new_block
;
1184 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1185 unsigned in_n_samples
, out_n_samples
;
1188 size_t leftover_length
= 0;
1192 pa_assert(input
->memblock
);
1194 /* Convert the incoming sample into the work sample format and place them
1195 * in to_work_format_buf. The leftover data is already converted, so it's
1196 * part of the output buffer. */
1198 have_leftover
= r
->leftover_in_to_work
;
1199 r
->leftover_in_to_work
= false;
1201 if (!have_leftover
&& (!r
->to_work_format_func
|| !input
->length
))
1203 else if (input
->length
<= 0)
1204 return &r
->to_work_format_buf
;
1206 in_n_samples
= out_n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1208 if (have_leftover
) {
1209 leftover_length
= r
->to_work_format_buf
.length
;
1210 out_n_samples
+= (unsigned) (leftover_length
/ r
->w_sz
);
1213 fit_buf(r
, &r
->to_work_format_buf
, r
->w_sz
* out_n_samples
, &r
->to_work_format_buf_size
, leftover_length
);
1215 src
= pa_memblock_acquire_chunk(input
);
1216 dst
= (uint8_t *) pa_memblock_acquire(r
->to_work_format_buf
.memblock
) + leftover_length
;
1218 if (r
->to_work_format_func
)
1219 r
->to_work_format_func(in_n_samples
, src
, dst
);
1221 memcpy(dst
, src
, input
->length
);
1223 pa_memblock_release(input
->memblock
);
1224 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1226 return &r
->to_work_format_buf
;
1229 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1230 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1232 size_t leftover_length
= 0;
1237 pa_assert(input
->memblock
);
1239 /* Remap channels and place the result in remap_buf. There may be leftover
1240 * data in the beginning of remap_buf. The leftover data is already
1241 * remapped, so it's not part of the input, it's part of the output. */
1243 have_leftover
= r
->leftover_in_remap
;
1244 r
->leftover_in_remap
= false;
1246 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1248 else if (input
->length
<= 0)
1249 return &r
->remap_buf
;
1251 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1252 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1254 if (have_leftover
) {
1255 leftover_length
= r
->remap_buf
.length
;
1256 out_n_frames
+= leftover_length
/ r
->w_fz
;
1259 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1260 fit_buf(r
, &r
->remap_buf
, out_n_samples
* r
->w_sz
, &r
->remap_buf_size
, leftover_length
);
1262 src
= pa_memblock_acquire_chunk(input
);
1263 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1265 if (r
->map_required
) {
1266 pa_remap_t
*remap
= &r
->remap
;
1268 pa_assert(remap
->do_remap
);
1269 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1272 memcpy(dst
, src
, input
->length
);
1274 pa_memblock_release(input
->memblock
);
1275 pa_memblock_release(r
->remap_buf
.memblock
);
1277 return &r
->remap_buf
;
1280 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1287 /* Store the leftover data. */
1288 fit_buf(r
, r
->leftover_buf
, len
, r
->leftover_buf_size
, 0);
1289 *r
->have_leftover
= true;
1291 dst
= pa_memblock_acquire(r
->leftover_buf
->memblock
);
1292 memmove(dst
, buf
, len
);
1293 pa_memblock_release(r
->leftover_buf
->memblock
);
1296 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1297 unsigned in_n_frames
, out_n_frames
, leftover_n_frames
;
1302 /* Resample the data and place the result in resample_buf. */
1304 if (!r
->impl
.resample
|| !input
->length
)
1307 in_n_frames
= (unsigned) (input
->length
/ r
->w_fz
);
1309 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1310 fit_buf(r
, &r
->resample_buf
, r
->w_fz
* out_n_frames
, &r
->resample_buf_size
, 0);
1312 leftover_n_frames
= r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1314 if (leftover_n_frames
> 0) {
1315 void *leftover_data
= (uint8_t *) pa_memblock_acquire_chunk(input
) + (in_n_frames
- leftover_n_frames
) * r
->w_fz
;
1316 save_leftover(r
, leftover_data
, leftover_n_frames
* r
->w_fz
);
1317 pa_memblock_release(input
->memblock
);
1320 r
->resample_buf
.length
= out_n_frames
* r
->w_fz
;
1322 return &r
->resample_buf
;
1325 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1326 unsigned n_samples
, n_frames
;
1332 /* Convert the data into the correct sample type and place the result in
1333 * from_work_format_buf. */
1335 if (!r
->from_work_format_func
|| !input
->length
)
1338 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1339 n_frames
= n_samples
/ r
->o_ss
.channels
;
1340 fit_buf(r
, &r
->from_work_format_buf
, r
->o_fz
* n_frames
, &r
->from_work_format_buf_size
, 0);
1342 src
= pa_memblock_acquire_chunk(input
);
1343 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1344 r
->from_work_format_func(n_samples
, src
, dst
);
1345 pa_memblock_release(input
->memblock
);
1346 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1348 return &r
->from_work_format_buf
;
1351 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1357 pa_assert(in
->length
);
1358 pa_assert(in
->memblock
);
1359 pa_assert(in
->length
% r
->i_fz
== 0);
1361 buf
= (pa_memchunk
*) in
;
1362 buf
= convert_to_work_format(r
, buf
);
1364 /* Try to save resampling effort: if we have more output channels than
1365 * input channels, do resampling first, then remapping. */
1366 if (r
->o_ss
.channels
<= r
->i_ss
.channels
) {
1367 buf
= remap_channels(r
, buf
);
1368 buf
= resample(r
, buf
);
1370 buf
= resample(r
, buf
);
1371 buf
= remap_channels(r
, buf
);
1375 buf
= convert_from_work_format(r
, buf
);
1379 pa_memblock_ref(buf
->memblock
);
1381 pa_memchunk_reset(buf
);
1383 pa_memchunk_reset(out
);
1386 /*** libsamplerate based implementation ***/
1388 #ifdef HAVE_LIBSAMPLERATE
1389 static unsigned libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1396 pa_assert(out_n_frames
);
1398 state
= r
->impl
.data
;
1399 memset(&data
, 0, sizeof(data
));
1401 data
.data_in
= pa_memblock_acquire_chunk(input
);
1402 data
.input_frames
= (long int) in_n_frames
;
1404 data
.data_out
= pa_memblock_acquire_chunk(output
);
1405 data
.output_frames
= (long int) *out_n_frames
;
1407 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1408 data
.end_of_input
= 0;
1410 pa_assert_se(src_process(state
, &data
) == 0);
1412 pa_memblock_release(input
->memblock
);
1413 pa_memblock_release(output
->memblock
);
1415 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1417 return in_n_frames
- data
.input_frames_used
;
1420 static void libsamplerate_update_rates(pa_resampler
*r
) {
1424 state
= r
->impl
.data
;
1425 pa_assert_se(src_set_ratio(state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1428 static void libsamplerate_reset(pa_resampler
*r
) {
1432 state
= r
->impl
.data
;
1433 pa_assert_se(src_reset(state
) == 0);
1436 static void libsamplerate_free(pa_resampler
*r
) {
1440 state
= r
->impl
.data
;
1445 static int libsamplerate_init(pa_resampler
*r
) {
1451 if (!(state
= src_new(r
->method
, r
->work_channels
, &err
)))
1454 r
->impl
.free
= libsamplerate_free
;
1455 r
->impl
.update_rates
= libsamplerate_update_rates
;
1456 r
->impl
.resample
= libsamplerate_resample
;
1457 r
->impl
.reset
= libsamplerate_reset
;
1458 r
->impl
.data
= state
;
1465 /*** speex based implementation ***/
1467 static unsigned speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1469 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1470 SpeexResamplerState
*state
;
1475 pa_assert(out_n_frames
);
1477 state
= r
->impl
.data
;
1479 in
= pa_memblock_acquire_chunk(input
);
1480 out
= pa_memblock_acquire_chunk(output
);
1482 pa_assert_se(speex_resampler_process_interleaved_float(state
, in
, &inf
, out
, &outf
) == 0);
1484 pa_memblock_release(input
->memblock
);
1485 pa_memblock_release(output
->memblock
);
1487 pa_assert(inf
== in_n_frames
);
1488 *out_n_frames
= outf
;
1493 static unsigned speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1495 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1496 SpeexResamplerState
*state
;
1501 pa_assert(out_n_frames
);
1503 state
= r
->impl
.data
;
1505 in
= pa_memblock_acquire_chunk(input
);
1506 out
= pa_memblock_acquire_chunk(output
);
1508 pa_assert_se(speex_resampler_process_interleaved_int(state
, in
, &inf
, out
, &outf
) == 0);
1510 pa_memblock_release(input
->memblock
);
1511 pa_memblock_release(output
->memblock
);
1513 pa_assert(inf
== in_n_frames
);
1514 *out_n_frames
= outf
;
1519 static void speex_update_rates(pa_resampler
*r
) {
1520 SpeexResamplerState
*state
;
1523 state
= r
->impl
.data
;
1525 pa_assert_se(speex_resampler_set_rate(state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1528 static void speex_reset(pa_resampler
*r
) {
1529 SpeexResamplerState
*state
;
1532 state
= r
->impl
.data
;
1534 pa_assert_se(speex_resampler_reset_mem(state
) == 0);
1537 static void speex_free(pa_resampler
*r
) {
1538 SpeexResamplerState
*state
;
1541 state
= r
->impl
.data
;
1545 speex_resampler_destroy(state
);
1548 static int speex_init(pa_resampler
*r
) {
1550 SpeexResamplerState
*state
;
1554 r
->impl
.free
= speex_free
;
1555 r
->impl
.update_rates
= speex_update_rates
;
1556 r
->impl
.reset
= speex_reset
;
1558 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1560 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1561 r
->impl
.resample
= speex_resample_int
;
1564 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1566 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1567 r
->impl
.resample
= speex_resample_float
;
1570 pa_log_info("Choosing speex quality setting %i.", q
);
1572 if (!(state
= speex_resampler_init(r
->work_channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1575 r
->impl
.data
= state
;
1581 /* Trivial implementation */
1583 static unsigned trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1584 unsigned i_index
, o_index
;
1586 struct trivial_data
*trivial_data
;
1591 pa_assert(out_n_frames
);
1593 trivial_data
= r
->impl
.data
;
1595 src
= pa_memblock_acquire_chunk(input
);
1596 dst
= pa_memblock_acquire_chunk(output
);
1598 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1599 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1600 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1602 if (i_index
>= in_n_frames
)
1605 pa_assert_fp(o_index
* r
->w_fz
< pa_memblock_get_length(output
->memblock
));
1607 memcpy((uint8_t*) dst
+ r
->w_fz
* o_index
, (uint8_t*) src
+ r
->w_fz
* i_index
, (int) r
->w_fz
);
1610 pa_memblock_release(input
->memblock
);
1611 pa_memblock_release(output
->memblock
);
1613 *out_n_frames
= o_index
;
1615 trivial_data
->i_counter
+= in_n_frames
;
1617 /* Normalize counters */
1618 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1619 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1621 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1622 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1628 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1629 struct trivial_data
*trivial_data
;
1632 trivial_data
= r
->impl
.data
;
1634 trivial_data
->i_counter
= 0;
1635 trivial_data
->o_counter
= 0;
1638 static int trivial_init(pa_resampler
*r
) {
1639 struct trivial_data
*trivial_data
;
1642 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1644 r
->impl
.resample
= trivial_resample
;
1645 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1646 r
->impl
.reset
= trivial_update_rates_or_reset
;
1647 r
->impl
.data
= trivial_data
;
1652 /* Peak finder implementation */
1654 static unsigned peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1655 unsigned c
, o_index
= 0;
1656 unsigned i
, i_end
= 0;
1658 struct peaks_data
*peaks_data
;
1663 pa_assert(out_n_frames
);
1665 peaks_data
= r
->impl
.data
;
1666 src
= pa_memblock_acquire_chunk(input
);
1667 dst
= pa_memblock_acquire_chunk(output
);
1669 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1670 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1672 while (i_end
< in_n_frames
) {
1673 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1674 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1676 pa_assert_fp(o_index
* r
->w_fz
< pa_memblock_get_length(output
->memblock
));
1678 /* 1ch float is treated separately, because that is the common case */
1679 if (r
->work_channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1680 float *s
= (float*) src
+ i
;
1681 float *d
= (float*) dst
+ o_index
;
1683 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1684 float n
= fabsf(*s
++);
1686 if (n
> peaks_data
->max_f
[0])
1687 peaks_data
->max_f
[0] = n
;
1691 *d
= peaks_data
->max_f
[0];
1692 peaks_data
->max_f
[0] = 0;
1693 o_index
++, peaks_data
->o_counter
++;
1695 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1696 int16_t *s
= (int16_t*) src
+ r
->work_channels
* i
;
1697 int16_t *d
= (int16_t*) dst
+ r
->work_channels
* o_index
;
1699 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1700 for (c
= 0; c
< r
->work_channels
; c
++) {
1701 int16_t n
= abs(*s
++);
1703 if (n
> peaks_data
->max_i
[c
])
1704 peaks_data
->max_i
[c
] = n
;
1708 for (c
= 0; c
< r
->work_channels
; c
++, d
++) {
1709 *d
= peaks_data
->max_i
[c
];
1710 peaks_data
->max_i
[c
] = 0;
1712 o_index
++, peaks_data
->o_counter
++;
1715 float *s
= (float*) src
+ r
->work_channels
* i
;
1716 float *d
= (float*) dst
+ r
->work_channels
* o_index
;
1718 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1719 for (c
= 0; c
< r
->work_channels
; c
++) {
1720 float n
= fabsf(*s
++);
1722 if (n
> peaks_data
->max_f
[c
])
1723 peaks_data
->max_f
[c
] = n
;
1727 for (c
= 0; c
< r
->work_channels
; c
++, d
++) {
1728 *d
= peaks_data
->max_f
[c
];
1729 peaks_data
->max_f
[c
] = 0;
1731 o_index
++, peaks_data
->o_counter
++;
1736 pa_memblock_release(input
->memblock
);
1737 pa_memblock_release(output
->memblock
);
1739 *out_n_frames
= o_index
;
1741 peaks_data
->i_counter
+= in_n_frames
;
1743 /* Normalize counters */
1744 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1745 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1747 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1748 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1754 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1755 struct peaks_data
*peaks_data
;
1758 peaks_data
= r
->impl
.data
;
1760 peaks_data
->i_counter
= 0;
1761 peaks_data
->o_counter
= 0;
1764 static int peaks_init(pa_resampler
*r
) {
1765 struct peaks_data
*peaks_data
;
1767 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1768 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1770 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1772 r
->impl
.resample
= peaks_resample
;
1773 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1774 r
->impl
.reset
= peaks_update_rates_or_reset
;
1775 r
->impl
.data
= peaks_data
;
1780 /*** ffmpeg based implementation ***/
1782 static unsigned ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1783 unsigned used_frames
= 0, c
;
1784 int previous_consumed_frames
= -1;
1785 struct ffmpeg_data
*ffmpeg_data
;
1790 pa_assert(out_n_frames
);
1792 ffmpeg_data
= r
->impl
.data
;
1794 for (c
= 0; c
< r
->work_channels
; c
++) {
1797 int16_t *p
, *t
, *k
, *q
, *s
;
1798 int consumed_frames
;
1800 /* Allocate a new block */
1801 b
= pa_memblock_new(r
->mempool
, in_n_frames
* sizeof(int16_t));
1802 p
= pa_memblock_acquire(b
);
1804 /* Now copy the input data, splitting up channels */
1805 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1807 for (u
= 0; u
< in_n_frames
; u
++) {
1809 t
+= r
->work_channels
;
1812 pa_memblock_release(input
->memblock
);
1814 /* Allocate buffer for the result */
1815 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1816 q
= pa_memblock_acquire(w
);
1819 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1822 (int) in_n_frames
, (int) *out_n_frames
,
1823 c
>= (unsigned) (r
->work_channels
-1));
1825 pa_memblock_release(b
);
1826 pa_memblock_unref(b
);
1828 pa_assert(consumed_frames
<= (int) in_n_frames
);
1829 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1830 previous_consumed_frames
= consumed_frames
;
1832 /* And place the results in the output buffer */
1833 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1834 for (u
= 0; u
< used_frames
; u
++) {
1837 s
+= r
->work_channels
;
1839 pa_memblock_release(output
->memblock
);
1840 pa_memblock_release(w
);
1841 pa_memblock_unref(w
);
1844 *out_n_frames
= used_frames
;
1846 return in_n_frames
- previous_consumed_frames
;
1849 static void ffmpeg_free(pa_resampler
*r
) {
1850 struct ffmpeg_data
*ffmpeg_data
;
1854 ffmpeg_data
= r
->impl
.data
;
1855 if (ffmpeg_data
->state
)
1856 av_resample_close(ffmpeg_data
->state
);
1859 static int ffmpeg_init(pa_resampler
*r
) {
1860 struct ffmpeg_data
*ffmpeg_data
;
1864 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1866 /* We could probably implement different quality levels by
1867 * adjusting the filter parameters here. However, ffmpeg
1868 * internally only uses these hardcoded values, so let's use them
1869 * here for now as well until ffmpeg makes this configurable. */
1871 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1874 r
->impl
.free
= ffmpeg_free
;
1875 r
->impl
.resample
= ffmpeg_resample
;
1876 r
->impl
.data
= (void *) ffmpeg_data
;
1881 /*** copy (noop) implementation ***/
1883 static int copy_init(pa_resampler
*r
) {
1886 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);