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 struct ffmpeg_data
{ /* data specific to ffmpeg */
94 struct AVResampleContext
*state
;
95 pa_memchunk buf
[PA_CHANNELS_MAX
];
98 static int copy_init(pa_resampler
*r
);
99 static int trivial_init(pa_resampler
*r
);
101 static int speex_init(pa_resampler
*r
);
103 static int ffmpeg_init(pa_resampler
*r
);
104 static int peaks_init(pa_resampler
*r
);
105 #ifdef HAVE_LIBSAMPLERATE
106 static int libsamplerate_init(pa_resampler
*r
);
109 static void calc_map_table(pa_resampler
*r
);
111 static int (* const init_table
[])(pa_resampler
*r
) = {
112 #ifdef HAVE_LIBSAMPLERATE
113 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
114 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
115 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
116 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
117 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
119 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
120 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
121 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
122 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
123 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
125 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
127 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
128 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
129 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
130 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
131 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
132 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
133 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
134 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
135 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
136 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
137 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
138 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
139 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
140 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
141 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
142 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
143 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
154 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
155 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
156 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
157 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
158 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
159 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
160 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
165 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
166 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
167 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
168 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
169 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
170 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
171 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
173 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
174 [PA_RESAMPLER_AUTO
] = NULL
,
175 [PA_RESAMPLER_COPY
] = copy_init
,
176 [PA_RESAMPLER_PEAKS
] = peaks_init
,
179 static pa_resample_method_t
choose_auto_resampler(pa_resample_flags_t flags
) {
180 pa_resample_method_t method
;
182 if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1))
183 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
184 else if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
185 method
= PA_RESAMPLER_TRIVIAL
;
187 method
= PA_RESAMPLER_FFMPEG
;
192 static pa_resample_method_t
pa_resampler_fix_method(
193 pa_resample_flags_t flags
,
194 pa_resample_method_t method
,
195 const uint32_t rate_a
,
196 const uint32_t rate_b
) {
198 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
199 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
200 pa_assert(method
>= 0);
201 pa_assert(method
< PA_RESAMPLER_MAX
);
203 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
204 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
205 method
= PA_RESAMPLER_COPY
;
208 if (!pa_resample_method_supported(method
)) {
209 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
210 method
= PA_RESAMPLER_AUTO
;
214 case PA_RESAMPLER_COPY
:
215 if (rate_a
!= rate_b
) {
216 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
217 method
= PA_RESAMPLER_AUTO
;
220 /* Else fall through */
221 case PA_RESAMPLER_FFMPEG
:
222 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
223 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
224 method
= PA_RESAMPLER_AUTO
;
228 /* The Peaks resampler only supports downsampling.
229 * Revert to auto if we are upsampling */
230 case PA_RESAMPLER_PEAKS
:
231 if (rate_a
< rate_b
) {
232 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
233 method
= PA_RESAMPLER_AUTO
;
241 if (method
== PA_RESAMPLER_AUTO
)
242 method
= choose_auto_resampler(flags
);
247 /* Return true if a is a more precise sample format than b, else return false */
248 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
249 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
250 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
259 case PA_SAMPLE_S16LE
:
260 case PA_SAMPLE_S16BE
:
261 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
267 case PA_SAMPLE_S24LE
:
268 case PA_SAMPLE_S24BE
:
269 case PA_SAMPLE_S24_32LE
:
270 case PA_SAMPLE_S24_32BE
:
271 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
272 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
278 case PA_SAMPLE_FLOAT32LE
:
279 case PA_SAMPLE_FLOAT32BE
:
280 case PA_SAMPLE_S32LE
:
281 case PA_SAMPLE_S32BE
:
282 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
283 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
294 static pa_sample_format_t
pa_resampler_choose_work_format(
295 pa_resample_method_t method
,
296 pa_sample_format_t a
,
297 pa_sample_format_t b
,
299 pa_sample_format_t work_format
;
301 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
302 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
303 pa_assert(method
>= 0);
304 pa_assert(method
< PA_RESAMPLER_MAX
);
306 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
307 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
310 /* This block is for resampling functions that only
311 * support the S16 sample format. */
312 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
313 case PA_RESAMPLER_FFMPEG
:
314 work_format
= PA_SAMPLE_S16NE
;
317 /* This block is for resampling functions that support
318 * any sample format. */
319 case PA_RESAMPLER_COPY
: /* fall through */
320 case PA_RESAMPLER_TRIVIAL
:
321 if (!map_required
&& a
== b
) {
325 /* Else fall trough */
326 case PA_RESAMPLER_PEAKS
:
327 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
328 work_format
= PA_SAMPLE_S16NE
;
329 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
330 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
331 work_format
= PA_SAMPLE_FLOAT32NE
;
333 work_format
= PA_SAMPLE_S16NE
;
337 work_format
= PA_SAMPLE_FLOAT32NE
;
343 pa_resampler
* pa_resampler_new(
345 const pa_sample_spec
*a
,
346 const pa_channel_map
*am
,
347 const pa_sample_spec
*b
,
348 const pa_channel_map
*bm
,
349 pa_resample_method_t method
,
350 pa_resample_flags_t flags
) {
352 pa_resampler
*r
= NULL
;
357 pa_assert(pa_sample_spec_valid(a
));
358 pa_assert(pa_sample_spec_valid(b
));
359 pa_assert(method
>= 0);
360 pa_assert(method
< PA_RESAMPLER_MAX
);
362 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
364 r
= pa_xnew0(pa_resampler
, 1);
369 /* Fill sample specs */
373 /* set up the remap structure */
374 r
->remap
.i_ss
= &r
->i_ss
;
375 r
->remap
.o_ss
= &r
->o_ss
;
376 r
->remap
.format
= &r
->work_format
;
380 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
385 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
388 r
->i_fz
= pa_frame_size(a
);
389 r
->o_fz
= pa_frame_size(b
);
393 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
395 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
397 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
399 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
401 if (r
->i_ss
.format
!= r
->work_format
) {
402 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
403 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
406 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
407 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
412 if (r
->o_ss
.format
!= r
->work_format
) {
413 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
414 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
417 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
418 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
423 /* initialize implementation */
424 if (init_table
[method
](r
) < 0)
435 void pa_resampler_free(pa_resampler
*r
) {
441 pa_xfree(r
->impl
.data
);
443 if (r
->to_work_format_buf
.memblock
)
444 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
445 if (r
->remap_buf
.memblock
)
446 pa_memblock_unref(r
->remap_buf
.memblock
);
447 if (r
->resample_buf
.memblock
)
448 pa_memblock_unref(r
->resample_buf
.memblock
);
449 if (r
->from_work_format_buf
.memblock
)
450 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
455 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
458 pa_assert(r
->impl
.update_rates
);
460 if (r
->i_ss
.rate
== rate
)
465 r
->impl
.update_rates(r
);
468 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
471 pa_assert(r
->impl
.update_rates
);
473 if (r
->o_ss
.rate
== rate
)
478 r
->impl
.update_rates(r
);
481 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
484 /* Let's round up here to make it more likely that the caller will get at
485 * least out_length amount of data from pa_resampler_run().
487 * We don't take the leftover into account here. If we did, then it might
488 * be in theory possible that this function would return 0 and
489 * pa_resampler_run() would also return 0. That could lead to infinite
490 * loops. When the leftover is ignored here, such loops would eventually
491 * terminate, because the leftover would grow each round, finally
492 * surpassing the minimum input threshold of the resampler. */
493 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
;
496 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
501 /* Let's round up here to ensure that the caller will always allocate big
502 * enough output buffer. */
504 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
506 if (r
->remap_buf_contains_leftover_data
)
507 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
509 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
512 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
513 size_t block_size_max
;
514 pa_sample_spec max_ss
;
520 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
522 /* We deduce the "largest" sample spec we're using during the
524 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
526 /* We silently assume that the format enum is ordered by size */
527 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
528 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
530 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
532 max_fs
= pa_frame_size(&max_ss
);
533 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
535 if (r
->remap_buf_contains_leftover_data
)
536 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
538 block_size_max
= ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
540 if (block_size_max
> 0)
541 return block_size_max
;
543 /* A single input frame may result in so much output that it doesn't
544 * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
545 * this case the max block size will be set to one frame, and some
546 * memory will be probably be allocated with malloc() instead of using
549 * XXX: Should we support this case at all? We could also refuse to
550 * create resamplers whose max block size would exceed the memory pool
551 * block size. In this case also updating the resampler rate should
552 * fail if the new rate would cause an excessive max block size (in
553 * which case the stream would probably have to be killed). */
557 void pa_resampler_reset(pa_resampler
*r
) {
563 r
->remap_buf_contains_leftover_data
= false;
566 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
572 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
578 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
584 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
590 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
596 static const char * const resample_methods
[] = {
597 "src-sinc-best-quality",
598 "src-sinc-medium-quality",
600 "src-zero-order-hold",
631 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
633 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
636 return resample_methods
[m
];
639 int pa_resample_method_supported(pa_resample_method_t m
) {
641 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
644 #ifndef HAVE_LIBSAMPLERATE
645 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
650 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
652 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
659 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
660 pa_resample_method_t m
;
664 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
665 if (pa_streq(string
, resample_methods
[m
]))
668 if (pa_streq(string
, "speex-fixed"))
669 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
671 if (pa_streq(string
, "speex-float"))
672 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
674 return PA_RESAMPLER_INVALID
;
677 static bool on_left(pa_channel_position_t p
) {
680 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
681 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
682 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
683 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
684 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
685 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
688 static bool on_right(pa_channel_position_t p
) {
691 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
692 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
693 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
694 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
695 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
696 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
699 static bool on_center(pa_channel_position_t p
) {
702 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
703 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
704 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
705 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
706 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
709 static bool on_lfe(pa_channel_position_t p
) {
711 p
== PA_CHANNEL_POSITION_LFE
;
714 static bool on_front(pa_channel_position_t p
) {
716 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
717 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
718 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
719 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
720 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
721 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
722 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
723 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
726 static bool on_rear(pa_channel_position_t p
) {
728 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
729 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
730 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
731 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
732 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
733 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
736 static bool on_side(pa_channel_position_t p
) {
738 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
739 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
740 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
750 static int front_rear_side(pa_channel_position_t p
) {
760 static void calc_map_table(pa_resampler
*r
) {
763 bool ic_connected
[PA_CHANNELS_MAX
];
771 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
)))))
776 n_oc
= r
->o_ss
.channels
;
777 n_ic
= r
->i_ss
.channels
;
779 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
780 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
782 memset(ic_connected
, 0, sizeof(ic_connected
));
783 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
785 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
788 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
789 m
->map_table_f
[oc
][oc
] = 1.0f
;
791 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
793 for (oc
= 0; oc
< n_oc
; oc
++) {
794 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
796 for (ic
= 0; ic
< n_ic
; ic
++) {
797 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
799 /* We shall not do any remixing. Hence, just check by name */
801 m
->map_table_f
[oc
][ic
] = 1.0f
;
806 /* OK, we shall do the full monty: upmixing and downmixing. Our
807 * algorithm is relatively simple, does not do spacialization, delay
808 * elements or apply lowpass filters for LFE. Patches are always
809 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
810 * probably wouldn't make any sense anyway.)
812 * This code is not idempotent: downmixing an upmixed stereo stream is
813 * not identical to the original. The volume will not match, and the
814 * two channels will be a linear combination of both.
816 * This is loosely based on random suggestions found on the Internet,
818 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
821 * The algorithm works basically like this:
823 * 1) Connect all channels with matching names.
826 * S:Mono: Copy into all D:channels
827 * D:Mono: Avg all S:channels
829 * 3) Mix D:Left, D:Right:
830 * D:Left: If not connected, avg all S:Left
831 * D:Right: If not connected, avg all S:Right
834 * If not connected, avg all S:Center
835 * If still not connected, avg all S:Left, S:Right
838 * If not connected, avg all S:*
840 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
841 * connected, mix into all D:left and all D:right channels. Gain is
844 * 7) Make sure S:Center, S:LFE is used:
846 * S:Center, S:LFE: If not connected, mix into all D:left, all
847 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
848 * for LFE. C-front is only mixed into L-front/R-front if available,
849 * otherwise into all L/R channels. Similarly for C-rear.
851 * 8) Normalize each row in the matrix such that the sum for each row is
852 * not larger than 1.0 in order to avoid clipping.
854 * S: and D: shall relate to the source resp. destination channels.
856 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
857 * rear if needed. For 4: we try to find some suitable C source for C,
858 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
859 * channels. For 6: the rear channels should not be dropped entirely,
860 * however have only minimal impact. For 7: movies usually encode
861 * speech on the center channel. Thus we have to make sure this channel
862 * is distributed to L and R if not available in the output. Also, LFE
863 * is used to achieve a greater dynamic range, and thus we should try
864 * to do our best to pass it to L+R.
871 ic_unconnected_left
= 0,
872 ic_unconnected_right
= 0,
873 ic_unconnected_center
= 0,
874 ic_unconnected_lfe
= 0;
875 bool ic_unconnected_center_mixed_in
= 0;
879 for (ic
= 0; ic
< n_ic
; ic
++) {
880 if (on_left(r
->i_cm
.map
[ic
]))
882 if (on_right(r
->i_cm
.map
[ic
]))
884 if (on_center(r
->i_cm
.map
[ic
]))
888 for (oc
= 0; oc
< n_oc
; oc
++) {
889 bool oc_connected
= false;
890 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
892 for (ic
= 0; ic
< n_ic
; ic
++) {
893 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
895 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
896 m
->map_table_f
[oc
][ic
] = 1.0f
;
899 ic_connected
[ic
] = true;
901 else if (b
== PA_CHANNEL_POSITION_MONO
) {
902 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
905 ic_connected
[ic
] = true;
910 /* Try to find matching input ports for this output port */
914 /* We are not connected and on the left side, let's
915 * average all left side input channels. */
918 for (ic
= 0; ic
< n_ic
; ic
++)
919 if (on_left(r
->i_cm
.map
[ic
])) {
920 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
921 ic_connected
[ic
] = true;
924 /* We ignore the case where there is no left input channel.
925 * Something is really wrong in this case anyway. */
927 } else if (on_right(b
)) {
929 /* We are not connected and on the right side, let's
930 * average all right side input channels. */
933 for (ic
= 0; ic
< n_ic
; ic
++)
934 if (on_right(r
->i_cm
.map
[ic
])) {
935 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
936 ic_connected
[ic
] = true;
939 /* We ignore the case where there is no right input
940 * channel. Something is really wrong in this case anyway.
943 } else if (on_center(b
)) {
947 /* We are not connected and at the center. Let's average
948 * all center input channels. */
950 for (ic
= 0; ic
< n_ic
; ic
++)
951 if (on_center(r
->i_cm
.map
[ic
])) {
952 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
953 ic_connected
[ic
] = true;
956 } else if (ic_left
+ ic_right
> 0) {
958 /* Hmm, no center channel around, let's synthesize it
959 * by mixing L and R.*/
961 for (ic
= 0; ic
< n_ic
; ic
++)
962 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
963 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
964 ic_connected
[ic
] = true;
968 /* We ignore the case where there is not even a left or
969 * right input channel. Something is really wrong in this
972 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
974 /* We are not connected and an LFE. Let's average all
975 * channels for LFE. */
977 for (ic
= 0; ic
< n_ic
; ic
++)
978 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
980 /* Please note that a channel connected to LFE doesn't
981 * really count as connected. */
986 for (ic
= 0; ic
< n_ic
; ic
++) {
987 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
989 if (ic_connected
[ic
])
993 ic_unconnected_left
++;
994 else if (on_right(a
))
995 ic_unconnected_right
++;
996 else if (on_center(a
))
997 ic_unconnected_center
++;
999 ic_unconnected_lfe
++;
1002 for (ic
= 0; ic
< n_ic
; ic
++) {
1003 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
1005 if (ic_connected
[ic
])
1008 for (oc
= 0; oc
< n_oc
; oc
++) {
1009 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
1011 if (on_left(a
) && on_left(b
))
1012 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1014 else if (on_right(a
) && on_right(b
))
1015 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1017 else if (on_center(a
) && on_center(b
)) {
1018 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1019 ic_unconnected_center_mixed_in
= true;
1021 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1022 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1026 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1027 unsigned ncenter
[PA_CHANNELS_MAX
];
1028 bool found_frs
[PA_CHANNELS_MAX
];
1030 memset(ncenter
, 0, sizeof(ncenter
));
1031 memset(found_frs
, 0, sizeof(found_frs
));
1033 /* Hmm, as it appears there was no center channel we
1034 could mix our center channel in. In this case, mix it into
1035 left and right. Using .5 as the factor. */
1037 for (ic
= 0; ic
< n_ic
; ic
++) {
1039 if (ic_connected
[ic
])
1042 if (!on_center(r
->i_cm
.map
[ic
]))
1045 for (oc
= 0; oc
< n_oc
; oc
++) {
1047 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1050 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1051 found_frs
[ic
] = true;
1056 for (oc
= 0; oc
< n_oc
; oc
++) {
1058 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1061 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1066 for (oc
= 0; oc
< n_oc
; oc
++) {
1068 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1071 if (ncenter
[oc
] <= 0)
1074 for (ic
= 0; ic
< n_ic
; ic
++) {
1076 if (!on_center(r
->i_cm
.map
[ic
]))
1079 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1080 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1086 for (oc
= 0; oc
< n_oc
; oc
++) {
1088 for (ic
= 0; ic
< n_ic
; ic
++)
1089 sum
+= m
->map_table_f
[oc
][ic
];
1092 for (ic
= 0; ic
< n_ic
; ic
++)
1093 m
->map_table_f
[oc
][ic
] /= sum
;
1096 /* make an 16:16 int version of the matrix */
1097 for (oc
= 0; oc
< n_oc
; oc
++)
1098 for (ic
= 0; ic
< n_ic
; ic
++)
1099 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1101 s
= pa_strbuf_new();
1103 pa_strbuf_printf(s
, " ");
1104 for (ic
= 0; ic
< n_ic
; ic
++)
1105 pa_strbuf_printf(s
, " I%02u ", ic
);
1106 pa_strbuf_puts(s
, "\n +");
1108 for (ic
= 0; ic
< n_ic
; ic
++)
1109 pa_strbuf_printf(s
, "------");
1110 pa_strbuf_puts(s
, "\n");
1112 for (oc
= 0; oc
< n_oc
; oc
++) {
1113 pa_strbuf_printf(s
, "O%02u |", oc
);
1115 for (ic
= 0; ic
< n_ic
; ic
++)
1116 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1118 pa_strbuf_puts(s
, "\n");
1121 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1124 /* initialize the remapping function */
1128 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1134 pa_assert(input
->memblock
);
1136 /* Convert the incoming sample into the work sample format and place them
1137 * in to_work_format_buf. */
1139 if (!r
->to_work_format_func
|| !input
->length
)
1142 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1144 r
->to_work_format_buf
.index
= 0;
1145 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1147 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1148 if (r
->to_work_format_buf
.memblock
)
1149 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1151 r
->to_work_format_buf_samples
= n_samples
;
1152 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1155 src
= pa_memblock_acquire_chunk(input
);
1156 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1158 r
->to_work_format_func(n_samples
, src
, dst
);
1160 pa_memblock_release(input
->memblock
);
1161 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1163 return &r
->to_work_format_buf
;
1166 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1167 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1169 size_t leftover_length
= 0;
1174 pa_assert(input
->memblock
);
1176 /* Remap channels and place the result in remap_buf. There may be leftover
1177 * data in the beginning of remap_buf. The leftover data is already
1178 * remapped, so it's not part of the input, it's part of the output. */
1180 have_leftover
= r
->remap_buf_contains_leftover_data
;
1181 r
->remap_buf_contains_leftover_data
= false;
1183 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1185 else if (input
->length
<= 0)
1186 return &r
->remap_buf
;
1188 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1189 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1191 if (have_leftover
) {
1192 leftover_length
= r
->remap_buf
.length
;
1193 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1196 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1197 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1199 if (have_leftover
) {
1200 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1201 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1203 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1204 dst
= pa_memblock_acquire(new_block
);
1205 memcpy(dst
, src
, leftover_length
);
1206 pa_memblock_release(r
->remap_buf
.memblock
);
1207 pa_memblock_release(new_block
);
1209 pa_memblock_unref(r
->remap_buf
.memblock
);
1210 r
->remap_buf
.memblock
= new_block
;
1211 r
->remap_buf_size
= r
->remap_buf
.length
;
1215 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1216 if (r
->remap_buf
.memblock
)
1217 pa_memblock_unref(r
->remap_buf
.memblock
);
1219 r
->remap_buf_size
= r
->remap_buf
.length
;
1220 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1224 src
= pa_memblock_acquire_chunk(input
);
1225 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1227 if (r
->map_required
) {
1228 pa_remap_t
*remap
= &r
->remap
;
1230 pa_assert(remap
->do_remap
);
1231 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1234 memcpy(dst
, src
, input
->length
);
1236 pa_memblock_release(input
->memblock
);
1237 pa_memblock_release(r
->remap_buf
.memblock
);
1239 return &r
->remap_buf
;
1242 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1243 unsigned in_n_frames
, in_n_samples
;
1244 unsigned out_n_frames
, out_n_samples
;
1249 /* Resample the data and place the result in resample_buf. */
1251 if (!r
->impl
.resample
|| !input
->length
)
1254 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1255 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1257 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1258 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1260 r
->resample_buf
.index
= 0;
1261 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1263 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1264 if (r
->resample_buf
.memblock
)
1265 pa_memblock_unref(r
->resample_buf
.memblock
);
1267 r
->resample_buf_samples
= out_n_samples
;
1268 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1271 r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1272 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1274 return &r
->resample_buf
;
1277 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1278 unsigned n_samples
, n_frames
;
1284 /* Convert the data into the correct sample type and place the result in
1285 * from_work_format_buf. */
1287 if (!r
->from_work_format_func
|| !input
->length
)
1290 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1291 n_frames
= n_samples
/ r
->o_ss
.channels
;
1293 r
->from_work_format_buf
.index
= 0;
1294 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1296 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1297 if (r
->from_work_format_buf
.memblock
)
1298 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1300 r
->from_work_format_buf_samples
= n_samples
;
1301 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1304 src
= pa_memblock_acquire_chunk(input
);
1305 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1306 r
->from_work_format_func(n_samples
, src
, dst
);
1307 pa_memblock_release(input
->memblock
);
1308 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1310 return &r
->from_work_format_buf
;
1313 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1319 pa_assert(in
->length
);
1320 pa_assert(in
->memblock
);
1321 pa_assert(in
->length
% r
->i_fz
== 0);
1323 buf
= (pa_memchunk
*) in
;
1324 buf
= convert_to_work_format(r
, buf
);
1325 buf
= remap_channels(r
, buf
);
1326 buf
= resample(r
, buf
);
1329 buf
= convert_from_work_format(r
, buf
);
1333 pa_memblock_ref(buf
->memblock
);
1335 pa_memchunk_reset(buf
);
1337 pa_memchunk_reset(out
);
1340 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1347 /* Store the leftover to remap_buf. */
1349 r
->remap_buf
.length
= len
;
1351 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1352 if (r
->remap_buf
.memblock
)
1353 pa_memblock_unref(r
->remap_buf
.memblock
);
1355 r
->remap_buf_size
= r
->remap_buf
.length
;
1356 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1359 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1360 memcpy(dst
, buf
, r
->remap_buf
.length
);
1361 pa_memblock_release(r
->remap_buf
.memblock
);
1363 r
->remap_buf_contains_leftover_data
= true;
1366 /*** libsamplerate based implementation ***/
1368 #ifdef HAVE_LIBSAMPLERATE
1369 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1376 pa_assert(out_n_frames
);
1378 state
= r
->impl
.data
;
1379 memset(&data
, 0, sizeof(data
));
1381 data
.data_in
= pa_memblock_acquire_chunk(input
);
1382 data
.input_frames
= (long int) in_n_frames
;
1384 data
.data_out
= pa_memblock_acquire_chunk(output
);
1385 data
.output_frames
= (long int) *out_n_frames
;
1387 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1388 data
.end_of_input
= 0;
1390 pa_assert_se(src_process(state
, &data
) == 0);
1392 if (data
.input_frames_used
< in_n_frames
) {
1393 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1394 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1396 save_leftover(r
, leftover_data
, leftover_length
);
1399 pa_memblock_release(input
->memblock
);
1400 pa_memblock_release(output
->memblock
);
1402 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1405 static void libsamplerate_update_rates(pa_resampler
*r
) {
1409 state
= r
->impl
.data
;
1410 pa_assert_se(src_set_ratio(state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1413 static void libsamplerate_reset(pa_resampler
*r
) {
1417 state
= r
->impl
.data
;
1418 pa_assert_se(src_reset(state
) == 0);
1421 static void libsamplerate_free(pa_resampler
*r
) {
1425 state
= r
->impl
.data
;
1430 static int libsamplerate_init(pa_resampler
*r
) {
1436 if (!(state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1439 r
->impl
.free
= libsamplerate_free
;
1440 r
->impl
.update_rates
= libsamplerate_update_rates
;
1441 r
->impl
.resample
= libsamplerate_resample
;
1442 r
->impl
.reset
= libsamplerate_reset
;
1443 r
->impl
.data
= state
;
1450 /*** speex based implementation ***/
1452 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1454 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1455 SpeexResamplerState
*state
;
1460 pa_assert(out_n_frames
);
1462 state
= r
->impl
.data
;
1464 in
= pa_memblock_acquire_chunk(input
);
1465 out
= pa_memblock_acquire_chunk(output
);
1467 pa_assert_se(speex_resampler_process_interleaved_float(state
, in
, &inf
, out
, &outf
) == 0);
1469 pa_memblock_release(input
->memblock
);
1470 pa_memblock_release(output
->memblock
);
1472 pa_assert(inf
== in_n_frames
);
1473 *out_n_frames
= outf
;
1476 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1478 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1479 SpeexResamplerState
*state
;
1484 pa_assert(out_n_frames
);
1486 state
= r
->impl
.data
;
1488 in
= pa_memblock_acquire_chunk(input
);
1489 out
= pa_memblock_acquire_chunk(output
);
1491 pa_assert_se(speex_resampler_process_interleaved_int(state
, in
, &inf
, out
, &outf
) == 0);
1493 pa_memblock_release(input
->memblock
);
1494 pa_memblock_release(output
->memblock
);
1496 pa_assert(inf
== in_n_frames
);
1497 *out_n_frames
= outf
;
1500 static void speex_update_rates(pa_resampler
*r
) {
1501 SpeexResamplerState
*state
;
1504 state
= r
->impl
.data
;
1506 pa_assert_se(speex_resampler_set_rate(state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1509 static void speex_reset(pa_resampler
*r
) {
1510 SpeexResamplerState
*state
;
1513 state
= r
->impl
.data
;
1515 pa_assert_se(speex_resampler_reset_mem(state
) == 0);
1518 static void speex_free(pa_resampler
*r
) {
1519 SpeexResamplerState
*state
;
1522 state
= r
->impl
.data
;
1526 speex_resampler_destroy(state
);
1529 static int speex_init(pa_resampler
*r
) {
1531 SpeexResamplerState
*state
;
1535 r
->impl
.free
= speex_free
;
1536 r
->impl
.update_rates
= speex_update_rates
;
1537 r
->impl
.reset
= speex_reset
;
1539 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1541 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1542 r
->impl
.resample
= speex_resample_int
;
1545 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1547 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1548 r
->impl
.resample
= speex_resample_float
;
1551 pa_log_info("Choosing speex quality setting %i.", q
);
1553 if (!(state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1556 r
->impl
.data
= state
;
1562 /* Trivial implementation */
1564 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1566 unsigned i_index
, o_index
;
1568 struct trivial_data
*trivial_data
;
1573 pa_assert(out_n_frames
);
1575 trivial_data
= r
->impl
.data
;
1576 fz
= r
->w_sz
* r
->o_ss
.channels
;
1578 src
= pa_memblock_acquire_chunk(input
);
1579 dst
= pa_memblock_acquire_chunk(output
);
1581 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1582 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1583 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1585 if (i_index
>= in_n_frames
)
1588 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1590 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1593 pa_memblock_release(input
->memblock
);
1594 pa_memblock_release(output
->memblock
);
1596 *out_n_frames
= o_index
;
1598 trivial_data
->i_counter
+= in_n_frames
;
1600 /* Normalize counters */
1601 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1602 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1604 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1605 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1609 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1610 struct trivial_data
*trivial_data
;
1613 trivial_data
= r
->impl
.data
;
1615 trivial_data
->i_counter
= 0;
1616 trivial_data
->o_counter
= 0;
1619 static int trivial_init(pa_resampler
*r
) {
1620 struct trivial_data
*trivial_data
;
1623 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1625 r
->impl
.resample
= trivial_resample
;
1626 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1627 r
->impl
.reset
= trivial_update_rates_or_reset
;
1628 r
->impl
.data
= trivial_data
;
1633 /* Peak finder implementation */
1635 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1636 unsigned c
, o_index
= 0;
1637 unsigned i
, i_end
= 0;
1639 struct peaks_data
*peaks_data
;
1644 pa_assert(out_n_frames
);
1646 peaks_data
= r
->impl
.data
;
1647 src
= pa_memblock_acquire_chunk(input
);
1648 dst
= pa_memblock_acquire_chunk(output
);
1650 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1651 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1653 while (i_end
< in_n_frames
) {
1654 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1655 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1657 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1659 /* 1ch float is treated separately, because that is the common case */
1660 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1661 float *s
= (float*) src
+ i
;
1662 float *d
= (float*) dst
+ o_index
;
1664 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1665 float n
= fabsf(*s
++);
1667 if (n
> peaks_data
->max_f
[0])
1668 peaks_data
->max_f
[0] = n
;
1672 *d
= peaks_data
->max_f
[0];
1673 peaks_data
->max_f
[0] = 0;
1674 o_index
++, peaks_data
->o_counter
++;
1676 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1677 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1678 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1680 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1681 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1682 int16_t n
= abs(*s
++);
1684 if (n
> peaks_data
->max_i
[c
])
1685 peaks_data
->max_i
[c
] = n
;
1689 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1690 *d
= peaks_data
->max_i
[c
];
1691 peaks_data
->max_i
[c
] = 0;
1693 o_index
++, peaks_data
->o_counter
++;
1696 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1697 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1699 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1700 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1701 float n
= fabsf(*s
++);
1703 if (n
> peaks_data
->max_f
[c
])
1704 peaks_data
->max_f
[c
] = n
;
1708 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1709 *d
= peaks_data
->max_f
[c
];
1710 peaks_data
->max_f
[c
] = 0;
1712 o_index
++, peaks_data
->o_counter
++;
1717 pa_memblock_release(input
->memblock
);
1718 pa_memblock_release(output
->memblock
);
1720 *out_n_frames
= o_index
;
1722 peaks_data
->i_counter
+= in_n_frames
;
1724 /* Normalize counters */
1725 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1726 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1728 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1729 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1733 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1734 struct peaks_data
*peaks_data
;
1737 peaks_data
= r
->impl
.data
;
1739 peaks_data
->i_counter
= 0;
1740 peaks_data
->o_counter
= 0;
1743 static int peaks_init(pa_resampler
*r
) {
1744 struct peaks_data
*peaks_data
;
1746 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1747 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1749 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1751 r
->impl
.resample
= peaks_resample
;
1752 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1753 r
->impl
.reset
= peaks_update_rates_or_reset
;
1754 r
->impl
.data
= peaks_data
;
1759 /*** ffmpeg based implementation ***/
1761 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1762 unsigned used_frames
= 0, c
;
1763 int previous_consumed_frames
= -1;
1764 struct ffmpeg_data
*ffmpeg_data
;
1769 pa_assert(out_n_frames
);
1771 ffmpeg_data
= r
->impl
.data
;
1773 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1776 int16_t *p
, *t
, *k
, *q
, *s
;
1777 int consumed_frames
;
1779 /* Allocate a new block */
1780 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1781 p
= pa_memblock_acquire(b
);
1783 /* Now copy the input data, splitting up channels */
1784 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1786 for (u
= 0; u
< in_n_frames
; u
++) {
1788 t
+= r
->o_ss
.channels
;
1791 pa_memblock_release(input
->memblock
);
1793 /* Allocate buffer for the result */
1794 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1795 q
= pa_memblock_acquire(w
);
1798 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1801 (int) in_n_frames
, (int) *out_n_frames
,
1802 c
>= (unsigned) (r
->o_ss
.channels
-1));
1804 pa_memblock_release(b
);
1805 pa_memblock_unref(b
);
1807 pa_assert(consumed_frames
<= (int) in_n_frames
);
1808 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1809 previous_consumed_frames
= consumed_frames
;
1811 /* And place the results in the output buffer */
1812 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1813 for (u
= 0; u
< used_frames
; u
++) {
1816 s
+= r
->o_ss
.channels
;
1818 pa_memblock_release(output
->memblock
);
1819 pa_memblock_release(w
);
1820 pa_memblock_unref(w
);
1823 if (previous_consumed_frames
< (int) in_n_frames
) {
1824 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1825 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1827 save_leftover(r
, leftover_data
, leftover_length
);
1828 pa_memblock_release(input
->memblock
);
1831 *out_n_frames
= used_frames
;
1834 static void ffmpeg_free(pa_resampler
*r
) {
1836 struct ffmpeg_data
*ffmpeg_data
;
1840 ffmpeg_data
= r
->impl
.data
;
1841 if (ffmpeg_data
->state
)
1842 av_resample_close(ffmpeg_data
->state
);
1844 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1845 if (ffmpeg_data
->buf
[c
].memblock
)
1846 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1849 static int ffmpeg_init(pa_resampler
*r
) {
1851 struct ffmpeg_data
*ffmpeg_data
;
1855 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1857 /* We could probably implement different quality levels by
1858 * adjusting the filter parameters here. However, ffmpeg
1859 * internally only uses these hardcoded values, so let's use them
1860 * here for now as well until ffmpeg makes this configurable. */
1862 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1865 r
->impl
.free
= ffmpeg_free
;
1866 r
->impl
.resample
= ffmpeg_resample
;
1867 r
->impl
.data
= (void *) ffmpeg_data
;
1869 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1870 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1875 /*** copy (noop) implementation ***/
1877 static int copy_init(pa_resampler
*r
) {
1880 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);