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
pa_resampler_fix_method(
180 pa_resample_flags_t flags
,
181 pa_resample_method_t method
,
182 const uint32_t rate_a
,
183 const uint32_t rate_b
) {
185 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
186 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
187 pa_assert(method
>= 0);
188 pa_assert(method
< PA_RESAMPLER_MAX
);
190 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
191 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
192 method
= PA_RESAMPLER_COPY
;
195 if (!pa_resample_method_supported(method
)) {
196 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
197 method
= PA_RESAMPLER_AUTO
;
201 case PA_RESAMPLER_COPY
:
202 if (rate_a
!= rate_b
) {
203 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
206 /* Else fall through */
207 case PA_RESAMPLER_FFMPEG
:
208 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
209 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
210 method
= PA_RESAMPLER_AUTO
;
214 /* The Peaks resampler only supports downsampling.
215 * Revert to auto if we are upsampling */
216 case PA_RESAMPLER_PEAKS
:
217 if (rate_a
< rate_b
) {
218 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
219 method
= PA_RESAMPLER_AUTO
;
227 if (method
== PA_RESAMPLER_AUTO
) {
229 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
231 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
232 method
= PA_RESAMPLER_TRIVIAL
;
234 method
= PA_RESAMPLER_FFMPEG
;
241 /* Return true if a is a more precise sample format than b, else return false */
242 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
243 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
244 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
253 case PA_SAMPLE_S16LE
:
254 case PA_SAMPLE_S16BE
:
255 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
261 case PA_SAMPLE_S24LE
:
262 case PA_SAMPLE_S24BE
:
263 case PA_SAMPLE_S24_32LE
:
264 case PA_SAMPLE_S24_32BE
:
265 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
266 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
272 case PA_SAMPLE_FLOAT32LE
:
273 case PA_SAMPLE_FLOAT32BE
:
274 case PA_SAMPLE_S32LE
:
275 case PA_SAMPLE_S32BE
:
276 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
277 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
288 static pa_sample_format_t
pa_resampler_choose_work_format(
289 pa_resample_method_t method
,
290 pa_sample_format_t a
,
291 pa_sample_format_t b
,
293 pa_sample_format_t work_format
;
295 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
296 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
297 pa_assert(method
>= 0);
298 pa_assert(method
< PA_RESAMPLER_MAX
);
300 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
301 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
304 /* This block is for resampling functions that only
305 * support the S16 sample format. */
306 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
307 case PA_RESAMPLER_FFMPEG
:
308 work_format
= PA_SAMPLE_S16NE
;
311 /* This block is for resampling functions that support
312 * any sample format. */
313 case PA_RESAMPLER_COPY
: /* fall through */
314 case PA_RESAMPLER_TRIVIAL
:
315 if (!map_required
&& a
== b
) {
319 /* Else fall trough */
320 case PA_RESAMPLER_PEAKS
:
321 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
322 work_format
= PA_SAMPLE_S16NE
;
323 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
324 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
325 work_format
= PA_SAMPLE_FLOAT32NE
;
327 work_format
= PA_SAMPLE_S16NE
;
331 work_format
= PA_SAMPLE_FLOAT32NE
;
337 pa_resampler
* pa_resampler_new(
339 const pa_sample_spec
*a
,
340 const pa_channel_map
*am
,
341 const pa_sample_spec
*b
,
342 const pa_channel_map
*bm
,
343 pa_resample_method_t method
,
344 pa_resample_flags_t flags
) {
346 pa_resampler
*r
= NULL
;
351 pa_assert(pa_sample_spec_valid(a
));
352 pa_assert(pa_sample_spec_valid(b
));
353 pa_assert(method
>= 0);
354 pa_assert(method
< PA_RESAMPLER_MAX
);
356 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
358 r
= pa_xnew0(pa_resampler
, 1);
363 /* Fill sample specs */
367 /* set up the remap structure */
368 r
->remap
.i_ss
= &r
->i_ss
;
369 r
->remap
.o_ss
= &r
->o_ss
;
370 r
->remap
.format
= &r
->work_format
;
374 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
379 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
382 r
->i_fz
= pa_frame_size(a
);
383 r
->o_fz
= pa_frame_size(b
);
387 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
389 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
391 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
393 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
395 if (r
->i_ss
.format
!= r
->work_format
) {
396 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
397 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
400 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
401 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
406 if (r
->o_ss
.format
!= r
->work_format
) {
407 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
408 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
411 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
412 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
417 /* initialize implementation */
418 if (init_table
[method
](r
) < 0)
429 void pa_resampler_free(pa_resampler
*r
) {
435 pa_xfree(r
->impl
.data
);
437 if (r
->to_work_format_buf
.memblock
)
438 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
439 if (r
->remap_buf
.memblock
)
440 pa_memblock_unref(r
->remap_buf
.memblock
);
441 if (r
->resample_buf
.memblock
)
442 pa_memblock_unref(r
->resample_buf
.memblock
);
443 if (r
->from_work_format_buf
.memblock
)
444 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
449 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
452 pa_assert(r
->impl
.update_rates
);
454 if (r
->i_ss
.rate
== rate
)
459 r
->impl
.update_rates(r
);
462 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
465 pa_assert(r
->impl
.update_rates
);
467 if (r
->o_ss
.rate
== rate
)
472 r
->impl
.update_rates(r
);
475 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
478 /* Let's round up here to make it more likely that the caller will get at
479 * least out_length amount of data from pa_resampler_run().
481 * We don't take the leftover into account here. If we did, then it might
482 * be in theory possible that this function would return 0 and
483 * pa_resampler_run() would also return 0. That could lead to infinite
484 * loops. When the leftover is ignored here, such loops would eventually
485 * terminate, because the leftover would grow each round, finally
486 * surpassing the minimum input threshold of the resampler. */
487 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
;
490 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
495 /* Let's round up here to ensure that the caller will always allocate big
496 * enough output buffer. */
498 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
500 if (r
->remap_buf_contains_leftover_data
)
501 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
503 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
506 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
507 size_t block_size_max
;
508 pa_sample_spec max_ss
;
514 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
516 /* We deduce the "largest" sample spec we're using during the
518 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
520 /* We silently assume that the format enum is ordered by size */
521 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
522 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
524 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
526 max_fs
= pa_frame_size(&max_ss
);
527 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
529 if (r
->remap_buf_contains_leftover_data
)
530 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
532 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
535 void pa_resampler_reset(pa_resampler
*r
) {
541 r
->remap_buf_contains_leftover_data
= false;
544 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
550 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
556 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
562 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
568 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
574 static const char * const resample_methods
[] = {
575 "src-sinc-best-quality",
576 "src-sinc-medium-quality",
578 "src-zero-order-hold",
609 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
611 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
614 return resample_methods
[m
];
617 int pa_resample_method_supported(pa_resample_method_t m
) {
619 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
622 #ifndef HAVE_LIBSAMPLERATE
623 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
628 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
630 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
637 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
638 pa_resample_method_t m
;
642 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
643 if (pa_streq(string
, resample_methods
[m
]))
646 if (pa_streq(string
, "speex-fixed"))
647 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
649 if (pa_streq(string
, "speex-float"))
650 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
652 return PA_RESAMPLER_INVALID
;
655 static bool on_left(pa_channel_position_t p
) {
658 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
659 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
660 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
661 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
662 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
663 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
666 static bool on_right(pa_channel_position_t p
) {
669 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
670 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
671 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
672 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
673 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
674 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
677 static bool on_center(pa_channel_position_t p
) {
680 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
681 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
682 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
683 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
684 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
687 static bool on_lfe(pa_channel_position_t p
) {
689 p
== PA_CHANNEL_POSITION_LFE
;
692 static bool on_front(pa_channel_position_t p
) {
694 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
695 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
696 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
697 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
698 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
699 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
700 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
701 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
704 static bool on_rear(pa_channel_position_t p
) {
706 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
707 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
708 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
709 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
710 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
711 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
714 static bool on_side(pa_channel_position_t p
) {
716 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
717 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
718 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
728 static int front_rear_side(pa_channel_position_t p
) {
738 static void calc_map_table(pa_resampler
*r
) {
741 bool ic_connected
[PA_CHANNELS_MAX
];
749 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
)))))
754 n_oc
= r
->o_ss
.channels
;
755 n_ic
= r
->i_ss
.channels
;
757 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
758 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
760 memset(ic_connected
, 0, sizeof(ic_connected
));
761 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
763 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
766 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
767 m
->map_table_f
[oc
][oc
] = 1.0f
;
769 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
771 for (oc
= 0; oc
< n_oc
; oc
++) {
772 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
774 for (ic
= 0; ic
< n_ic
; ic
++) {
775 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
777 /* We shall not do any remixing. Hence, just check by name */
779 m
->map_table_f
[oc
][ic
] = 1.0f
;
784 /* OK, we shall do the full monty: upmixing and downmixing. Our
785 * algorithm is relatively simple, does not do spacialization, delay
786 * elements or apply lowpass filters for LFE. Patches are always
787 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
788 * probably wouldn't make any sense anyway.)
790 * This code is not idempotent: downmixing an upmixed stereo stream is
791 * not identical to the original. The volume will not match, and the
792 * two channels will be a linear combination of both.
794 * This is loosely based on random suggestions found on the Internet,
796 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
799 * The algorithm works basically like this:
801 * 1) Connect all channels with matching names.
804 * S:Mono: Copy into all D:channels
805 * D:Mono: Avg all S:channels
807 * 3) Mix D:Left, D:Right:
808 * D:Left: If not connected, avg all S:Left
809 * D:Right: If not connected, avg all S:Right
812 * If not connected, avg all S:Center
813 * If still not connected, avg all S:Left, S:Right
816 * If not connected, avg all S:*
818 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
819 * connected, mix into all D:left and all D:right channels. Gain is
822 * 7) Make sure S:Center, S:LFE is used:
824 * S:Center, S:LFE: If not connected, mix into all D:left, all
825 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
826 * for LFE. C-front is only mixed into L-front/R-front if available,
827 * otherwise into all L/R channels. Similarly for C-rear.
829 * 8) Normalize each row in the matrix such that the sum for each row is
830 * not larger than 1.0 in order to avoid clipping.
832 * S: and D: shall relate to the source resp. destination channels.
834 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
835 * rear if needed. For 4: we try to find some suitable C source for C,
836 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
837 * channels. For 6: the rear channels should not be dropped entirely,
838 * however have only minimal impact. For 7: movies usually encode
839 * speech on the center channel. Thus we have to make sure this channel
840 * is distributed to L and R if not available in the output. Also, LFE
841 * is used to achieve a greater dynamic range, and thus we should try
842 * to do our best to pass it to L+R.
849 ic_unconnected_left
= 0,
850 ic_unconnected_right
= 0,
851 ic_unconnected_center
= 0,
852 ic_unconnected_lfe
= 0;
853 bool ic_unconnected_center_mixed_in
= 0;
857 for (ic
= 0; ic
< n_ic
; ic
++) {
858 if (on_left(r
->i_cm
.map
[ic
]))
860 if (on_right(r
->i_cm
.map
[ic
]))
862 if (on_center(r
->i_cm
.map
[ic
]))
866 for (oc
= 0; oc
< n_oc
; oc
++) {
867 bool oc_connected
= false;
868 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
870 for (ic
= 0; ic
< n_ic
; ic
++) {
871 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
873 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
874 m
->map_table_f
[oc
][ic
] = 1.0f
;
877 ic_connected
[ic
] = true;
879 else if (b
== PA_CHANNEL_POSITION_MONO
) {
880 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
883 ic_connected
[ic
] = true;
888 /* Try to find matching input ports for this output port */
892 /* We are not connected and on the left side, let's
893 * average all left side input channels. */
896 for (ic
= 0; ic
< n_ic
; ic
++)
897 if (on_left(r
->i_cm
.map
[ic
])) {
898 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
899 ic_connected
[ic
] = true;
902 /* We ignore the case where there is no left input channel.
903 * Something is really wrong in this case anyway. */
905 } else if (on_right(b
)) {
907 /* We are not connected and on the right side, let's
908 * average all right side input channels. */
911 for (ic
= 0; ic
< n_ic
; ic
++)
912 if (on_right(r
->i_cm
.map
[ic
])) {
913 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
914 ic_connected
[ic
] = true;
917 /* We ignore the case where there is no right input
918 * channel. Something is really wrong in this case anyway.
921 } else if (on_center(b
)) {
925 /* We are not connected and at the center. Let's average
926 * all center input channels. */
928 for (ic
= 0; ic
< n_ic
; ic
++)
929 if (on_center(r
->i_cm
.map
[ic
])) {
930 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
931 ic_connected
[ic
] = true;
934 } else if (ic_left
+ ic_right
> 0) {
936 /* Hmm, no center channel around, let's synthesize it
937 * by mixing L and R.*/
939 for (ic
= 0; ic
< n_ic
; ic
++)
940 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
941 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
942 ic_connected
[ic
] = true;
946 /* We ignore the case where there is not even a left or
947 * right input channel. Something is really wrong in this
950 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
952 /* We are not connected and an LFE. Let's average all
953 * channels for LFE. */
955 for (ic
= 0; ic
< n_ic
; ic
++)
956 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
958 /* Please note that a channel connected to LFE doesn't
959 * really count as connected. */
964 for (ic
= 0; ic
< n_ic
; ic
++) {
965 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
967 if (ic_connected
[ic
])
971 ic_unconnected_left
++;
972 else if (on_right(a
))
973 ic_unconnected_right
++;
974 else if (on_center(a
))
975 ic_unconnected_center
++;
977 ic_unconnected_lfe
++;
980 for (ic
= 0; ic
< n_ic
; ic
++) {
981 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
983 if (ic_connected
[ic
])
986 for (oc
= 0; oc
< n_oc
; oc
++) {
987 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
989 if (on_left(a
) && on_left(b
))
990 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
992 else if (on_right(a
) && on_right(b
))
993 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
995 else if (on_center(a
) && on_center(b
)) {
996 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
997 ic_unconnected_center_mixed_in
= true;
999 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1000 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1004 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1005 unsigned ncenter
[PA_CHANNELS_MAX
];
1006 bool found_frs
[PA_CHANNELS_MAX
];
1008 memset(ncenter
, 0, sizeof(ncenter
));
1009 memset(found_frs
, 0, sizeof(found_frs
));
1011 /* Hmm, as it appears there was no center channel we
1012 could mix our center channel in. In this case, mix it into
1013 left and right. Using .5 as the factor. */
1015 for (ic
= 0; ic
< n_ic
; ic
++) {
1017 if (ic_connected
[ic
])
1020 if (!on_center(r
->i_cm
.map
[ic
]))
1023 for (oc
= 0; oc
< n_oc
; oc
++) {
1025 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1028 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1029 found_frs
[ic
] = true;
1034 for (oc
= 0; oc
< n_oc
; oc
++) {
1036 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1039 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1044 for (oc
= 0; oc
< n_oc
; oc
++) {
1046 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1049 if (ncenter
[oc
] <= 0)
1052 for (ic
= 0; ic
< n_ic
; ic
++) {
1054 if (!on_center(r
->i_cm
.map
[ic
]))
1057 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1058 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1064 for (oc
= 0; oc
< n_oc
; oc
++) {
1066 for (ic
= 0; ic
< n_ic
; ic
++)
1067 sum
+= m
->map_table_f
[oc
][ic
];
1070 for (ic
= 0; ic
< n_ic
; ic
++)
1071 m
->map_table_f
[oc
][ic
] /= sum
;
1074 /* make an 16:16 int version of the matrix */
1075 for (oc
= 0; oc
< n_oc
; oc
++)
1076 for (ic
= 0; ic
< n_ic
; ic
++)
1077 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1079 s
= pa_strbuf_new();
1081 pa_strbuf_printf(s
, " ");
1082 for (ic
= 0; ic
< n_ic
; ic
++)
1083 pa_strbuf_printf(s
, " I%02u ", ic
);
1084 pa_strbuf_puts(s
, "\n +");
1086 for (ic
= 0; ic
< n_ic
; ic
++)
1087 pa_strbuf_printf(s
, "------");
1088 pa_strbuf_puts(s
, "\n");
1090 for (oc
= 0; oc
< n_oc
; oc
++) {
1091 pa_strbuf_printf(s
, "O%02u |", oc
);
1093 for (ic
= 0; ic
< n_ic
; ic
++)
1094 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1096 pa_strbuf_puts(s
, "\n");
1099 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1102 /* initialize the remapping function */
1106 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1112 pa_assert(input
->memblock
);
1114 /* Convert the incoming sample into the work sample format and place them
1115 * in to_work_format_buf. */
1117 if (!r
->to_work_format_func
|| !input
->length
)
1120 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1122 r
->to_work_format_buf
.index
= 0;
1123 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1125 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1126 if (r
->to_work_format_buf
.memblock
)
1127 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1129 r
->to_work_format_buf_samples
= n_samples
;
1130 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1133 src
= pa_memblock_acquire_chunk(input
);
1134 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1136 r
->to_work_format_func(n_samples
, src
, dst
);
1138 pa_memblock_release(input
->memblock
);
1139 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1141 return &r
->to_work_format_buf
;
1144 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1145 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1147 size_t leftover_length
= 0;
1152 pa_assert(input
->memblock
);
1154 /* Remap channels and place the result in remap_buf. There may be leftover
1155 * data in the beginning of remap_buf. The leftover data is already
1156 * remapped, so it's not part of the input, it's part of the output. */
1158 have_leftover
= r
->remap_buf_contains_leftover_data
;
1159 r
->remap_buf_contains_leftover_data
= false;
1161 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1163 else if (input
->length
<= 0)
1164 return &r
->remap_buf
;
1166 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1167 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1169 if (have_leftover
) {
1170 leftover_length
= r
->remap_buf
.length
;
1171 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1174 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1175 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1177 if (have_leftover
) {
1178 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1179 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1181 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1182 dst
= pa_memblock_acquire(new_block
);
1183 memcpy(dst
, src
, leftover_length
);
1184 pa_memblock_release(r
->remap_buf
.memblock
);
1185 pa_memblock_release(new_block
);
1187 pa_memblock_unref(r
->remap_buf
.memblock
);
1188 r
->remap_buf
.memblock
= new_block
;
1189 r
->remap_buf_size
= r
->remap_buf
.length
;
1193 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1194 if (r
->remap_buf
.memblock
)
1195 pa_memblock_unref(r
->remap_buf
.memblock
);
1197 r
->remap_buf_size
= r
->remap_buf
.length
;
1198 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1202 src
= pa_memblock_acquire_chunk(input
);
1203 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1205 if (r
->map_required
) {
1206 pa_remap_t
*remap
= &r
->remap
;
1208 pa_assert(remap
->do_remap
);
1209 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1212 memcpy(dst
, src
, input
->length
);
1214 pa_memblock_release(input
->memblock
);
1215 pa_memblock_release(r
->remap_buf
.memblock
);
1217 return &r
->remap_buf
;
1220 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1221 unsigned in_n_frames
, in_n_samples
;
1222 unsigned out_n_frames
, out_n_samples
;
1227 /* Resample the data and place the result in resample_buf. */
1229 if (!r
->impl
.resample
|| !input
->length
)
1232 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1233 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1235 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1236 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1238 r
->resample_buf
.index
= 0;
1239 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1241 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1242 if (r
->resample_buf
.memblock
)
1243 pa_memblock_unref(r
->resample_buf
.memblock
);
1245 r
->resample_buf_samples
= out_n_samples
;
1246 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1249 r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1250 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1252 return &r
->resample_buf
;
1255 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1256 unsigned n_samples
, n_frames
;
1262 /* Convert the data into the correct sample type and place the result in
1263 * from_work_format_buf. */
1265 if (!r
->from_work_format_func
|| !input
->length
)
1268 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1269 n_frames
= n_samples
/ r
->o_ss
.channels
;
1271 r
->from_work_format_buf
.index
= 0;
1272 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1274 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1275 if (r
->from_work_format_buf
.memblock
)
1276 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1278 r
->from_work_format_buf_samples
= n_samples
;
1279 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1282 src
= pa_memblock_acquire_chunk(input
);
1283 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1284 r
->from_work_format_func(n_samples
, src
, dst
);
1285 pa_memblock_release(input
->memblock
);
1286 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1288 return &r
->from_work_format_buf
;
1291 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1297 pa_assert(in
->length
);
1298 pa_assert(in
->memblock
);
1299 pa_assert(in
->length
% r
->i_fz
== 0);
1301 buf
= (pa_memchunk
*) in
;
1302 buf
= convert_to_work_format(r
, buf
);
1303 buf
= remap_channels(r
, buf
);
1304 buf
= resample(r
, buf
);
1307 buf
= convert_from_work_format(r
, buf
);
1311 pa_memblock_ref(buf
->memblock
);
1313 pa_memchunk_reset(buf
);
1315 pa_memchunk_reset(out
);
1318 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1325 /* Store the leftover to remap_buf. */
1327 r
->remap_buf
.length
= len
;
1329 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1330 if (r
->remap_buf
.memblock
)
1331 pa_memblock_unref(r
->remap_buf
.memblock
);
1333 r
->remap_buf_size
= r
->remap_buf
.length
;
1334 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1337 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1338 memcpy(dst
, buf
, r
->remap_buf
.length
);
1339 pa_memblock_release(r
->remap_buf
.memblock
);
1341 r
->remap_buf_contains_leftover_data
= true;
1344 /*** libsamplerate based implementation ***/
1346 #ifdef HAVE_LIBSAMPLERATE
1347 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1354 pa_assert(out_n_frames
);
1356 state
= r
->impl
.data
;
1357 memset(&data
, 0, sizeof(data
));
1359 data
.data_in
= pa_memblock_acquire_chunk(input
);
1360 data
.input_frames
= (long int) in_n_frames
;
1362 data
.data_out
= pa_memblock_acquire_chunk(output
);
1363 data
.output_frames
= (long int) *out_n_frames
;
1365 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1366 data
.end_of_input
= 0;
1368 pa_assert_se(src_process(state
, &data
) == 0);
1370 if (data
.input_frames_used
< in_n_frames
) {
1371 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1372 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1374 save_leftover(r
, leftover_data
, leftover_length
);
1377 pa_memblock_release(input
->memblock
);
1378 pa_memblock_release(output
->memblock
);
1380 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1383 static void libsamplerate_update_rates(pa_resampler
*r
) {
1387 state
= r
->impl
.data
;
1388 pa_assert_se(src_set_ratio(state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1391 static void libsamplerate_reset(pa_resampler
*r
) {
1395 state
= r
->impl
.data
;
1396 pa_assert_se(src_reset(state
) == 0);
1399 static void libsamplerate_free(pa_resampler
*r
) {
1403 state
= r
->impl
.data
;
1408 static int libsamplerate_init(pa_resampler
*r
) {
1414 if (!(state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1417 r
->impl
.free
= libsamplerate_free
;
1418 r
->impl
.update_rates
= libsamplerate_update_rates
;
1419 r
->impl
.resample
= libsamplerate_resample
;
1420 r
->impl
.reset
= libsamplerate_reset
;
1421 r
->impl
.data
= state
;
1428 /*** speex based implementation ***/
1430 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1432 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1433 SpeexResamplerState
*state
;
1438 pa_assert(out_n_frames
);
1440 state
= r
->impl
.data
;
1442 in
= pa_memblock_acquire_chunk(input
);
1443 out
= pa_memblock_acquire_chunk(output
);
1445 pa_assert_se(speex_resampler_process_interleaved_float(state
, in
, &inf
, out
, &outf
) == 0);
1447 pa_memblock_release(input
->memblock
);
1448 pa_memblock_release(output
->memblock
);
1450 pa_assert(inf
== in_n_frames
);
1451 *out_n_frames
= outf
;
1454 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1456 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1457 SpeexResamplerState
*state
;
1462 pa_assert(out_n_frames
);
1464 state
= r
->impl
.data
;
1466 in
= pa_memblock_acquire_chunk(input
);
1467 out
= pa_memblock_acquire_chunk(output
);
1469 pa_assert_se(speex_resampler_process_interleaved_int(state
, in
, &inf
, out
, &outf
) == 0);
1471 pa_memblock_release(input
->memblock
);
1472 pa_memblock_release(output
->memblock
);
1474 pa_assert(inf
== in_n_frames
);
1475 *out_n_frames
= outf
;
1478 static void speex_update_rates(pa_resampler
*r
) {
1479 SpeexResamplerState
*state
;
1482 state
= r
->impl
.data
;
1484 pa_assert_se(speex_resampler_set_rate(state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1487 static void speex_reset(pa_resampler
*r
) {
1488 SpeexResamplerState
*state
;
1491 state
= r
->impl
.data
;
1493 pa_assert_se(speex_resampler_reset_mem(state
) == 0);
1496 static void speex_free(pa_resampler
*r
) {
1497 SpeexResamplerState
*state
;
1500 state
= r
->impl
.data
;
1504 speex_resampler_destroy(state
);
1507 static int speex_init(pa_resampler
*r
) {
1509 SpeexResamplerState
*state
;
1513 r
->impl
.free
= speex_free
;
1514 r
->impl
.update_rates
= speex_update_rates
;
1515 r
->impl
.reset
= speex_reset
;
1517 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1519 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1520 r
->impl
.resample
= speex_resample_int
;
1523 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1525 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1526 r
->impl
.resample
= speex_resample_float
;
1529 pa_log_info("Choosing speex quality setting %i.", q
);
1531 if (!(state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1534 r
->impl
.data
= state
;
1540 /* Trivial implementation */
1542 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1544 unsigned i_index
, o_index
;
1546 struct trivial_data
*trivial_data
;
1551 pa_assert(out_n_frames
);
1553 trivial_data
= r
->impl
.data
;
1554 fz
= r
->w_sz
* r
->o_ss
.channels
;
1556 src
= pa_memblock_acquire_chunk(input
);
1557 dst
= pa_memblock_acquire_chunk(output
);
1559 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1560 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1561 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1563 if (i_index
>= in_n_frames
)
1566 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1568 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1571 pa_memblock_release(input
->memblock
);
1572 pa_memblock_release(output
->memblock
);
1574 *out_n_frames
= o_index
;
1576 trivial_data
->i_counter
+= in_n_frames
;
1578 /* Normalize counters */
1579 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1580 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1582 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1583 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1587 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1588 struct trivial_data
*trivial_data
;
1591 trivial_data
= r
->impl
.data
;
1593 trivial_data
->i_counter
= 0;
1594 trivial_data
->o_counter
= 0;
1597 static int trivial_init(pa_resampler
*r
) {
1598 struct trivial_data
*trivial_data
;
1601 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1603 r
->impl
.resample
= trivial_resample
;
1604 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1605 r
->impl
.reset
= trivial_update_rates_or_reset
;
1606 r
->impl
.data
= trivial_data
;
1611 /* Peak finder implementation */
1613 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1614 unsigned c
, o_index
= 0;
1615 unsigned i
, i_end
= 0;
1617 struct peaks_data
*peaks_data
;
1622 pa_assert(out_n_frames
);
1624 peaks_data
= r
->impl
.data
;
1625 src
= pa_memblock_acquire_chunk(input
);
1626 dst
= pa_memblock_acquire_chunk(output
);
1628 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1629 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1631 while (i_end
< in_n_frames
) {
1632 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1633 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1635 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1637 /* 1ch float is treated separately, because that is the common case */
1638 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1639 float *s
= (float*) src
+ i
;
1640 float *d
= (float*) dst
+ o_index
;
1642 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1643 float n
= fabsf(*s
++);
1645 if (n
> peaks_data
->max_f
[0])
1646 peaks_data
->max_f
[0] = n
;
1650 *d
= peaks_data
->max_f
[0];
1651 peaks_data
->max_f
[0] = 0;
1652 o_index
++, peaks_data
->o_counter
++;
1654 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1655 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1656 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1658 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1659 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1660 int16_t n
= abs(*s
++);
1662 if (n
> peaks_data
->max_i
[c
])
1663 peaks_data
->max_i
[c
] = n
;
1667 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1668 *d
= peaks_data
->max_i
[c
];
1669 peaks_data
->max_i
[c
] = 0;
1671 o_index
++, peaks_data
->o_counter
++;
1674 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1675 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1677 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1678 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1679 float n
= fabsf(*s
++);
1681 if (n
> peaks_data
->max_f
[c
])
1682 peaks_data
->max_f
[c
] = n
;
1686 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1687 *d
= peaks_data
->max_f
[c
];
1688 peaks_data
->max_f
[c
] = 0;
1690 o_index
++, peaks_data
->o_counter
++;
1695 pa_memblock_release(input
->memblock
);
1696 pa_memblock_release(output
->memblock
);
1698 *out_n_frames
= o_index
;
1700 peaks_data
->i_counter
+= in_n_frames
;
1702 /* Normalize counters */
1703 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1704 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1706 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1707 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1711 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1712 struct peaks_data
*peaks_data
;
1715 peaks_data
= r
->impl
.data
;
1717 peaks_data
->i_counter
= 0;
1718 peaks_data
->o_counter
= 0;
1721 static int peaks_init(pa_resampler
*r
) {
1722 struct peaks_data
*peaks_data
;
1724 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1725 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1727 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1729 r
->impl
.resample
= peaks_resample
;
1730 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1731 r
->impl
.reset
= peaks_update_rates_or_reset
;
1732 r
->impl
.data
= peaks_data
;
1737 /*** ffmpeg based implementation ***/
1739 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1740 unsigned used_frames
= 0, c
;
1741 int previous_consumed_frames
= -1;
1742 struct ffmpeg_data
*ffmpeg_data
;
1747 pa_assert(out_n_frames
);
1749 ffmpeg_data
= r
->impl
.data
;
1751 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1754 int16_t *p
, *t
, *k
, *q
, *s
;
1755 int consumed_frames
;
1757 /* Allocate a new block */
1758 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1759 p
= pa_memblock_acquire(b
);
1761 /* Now copy the input data, splitting up channels */
1762 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1764 for (u
= 0; u
< in_n_frames
; u
++) {
1766 t
+= r
->o_ss
.channels
;
1769 pa_memblock_release(input
->memblock
);
1771 /* Allocate buffer for the result */
1772 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1773 q
= pa_memblock_acquire(w
);
1776 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1779 (int) in_n_frames
, (int) *out_n_frames
,
1780 c
>= (unsigned) (r
->o_ss
.channels
-1));
1782 pa_memblock_release(b
);
1783 pa_memblock_unref(b
);
1785 pa_assert(consumed_frames
<= (int) in_n_frames
);
1786 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1787 previous_consumed_frames
= consumed_frames
;
1789 /* And place the results in the output buffer */
1790 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1791 for (u
= 0; u
< used_frames
; u
++) {
1794 s
+= r
->o_ss
.channels
;
1796 pa_memblock_release(output
->memblock
);
1797 pa_memblock_release(w
);
1798 pa_memblock_unref(w
);
1801 if (previous_consumed_frames
< (int) in_n_frames
) {
1802 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1803 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1805 save_leftover(r
, leftover_data
, leftover_length
);
1806 pa_memblock_release(input
->memblock
);
1809 *out_n_frames
= used_frames
;
1812 static void ffmpeg_free(pa_resampler
*r
) {
1814 struct ffmpeg_data
*ffmpeg_data
;
1818 ffmpeg_data
= r
->impl
.data
;
1819 if (ffmpeg_data
->state
)
1820 av_resample_close(ffmpeg_data
->state
);
1822 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1823 if (ffmpeg_data
->buf
[c
].memblock
)
1824 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1827 static int ffmpeg_init(pa_resampler
*r
) {
1829 struct ffmpeg_data
*ffmpeg_data
;
1833 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1835 /* We could probably implement different quality levels by
1836 * adjusting the filter parameters here. However, ffmpeg
1837 * internally only uses these hardcoded values, so let's use them
1838 * here for now as well until ffmpeg makes this configurable. */
1840 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1843 r
->impl
.free
= ffmpeg_free
;
1844 r
->impl
.resample
= ffmpeg_resample
;
1845 r
->impl
.data
= (void *) ffmpeg_data
;
1847 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1848 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1853 /*** copy (noop) implementation ***/
1855 static int copy_init(pa_resampler
*r
) {
1858 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);