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'.");
219 /* Else fall through */
220 case PA_RESAMPLER_FFMPEG
:
221 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
222 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
223 method
= PA_RESAMPLER_AUTO
;
227 /* The Peaks resampler only supports downsampling.
228 * Revert to auto if we are upsampling */
229 case PA_RESAMPLER_PEAKS
:
230 if (rate_a
< rate_b
) {
231 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
232 method
= PA_RESAMPLER_AUTO
;
240 if (method
== PA_RESAMPLER_AUTO
)
241 method
= choose_auto_resampler(flags
);
246 /* Return true if a is a more precise sample format than b, else return false */
247 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
248 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
249 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
258 case PA_SAMPLE_S16LE
:
259 case PA_SAMPLE_S16BE
:
260 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
266 case PA_SAMPLE_S24LE
:
267 case PA_SAMPLE_S24BE
:
268 case PA_SAMPLE_S24_32LE
:
269 case PA_SAMPLE_S24_32BE
:
270 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
271 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
277 case PA_SAMPLE_FLOAT32LE
:
278 case PA_SAMPLE_FLOAT32BE
:
279 case PA_SAMPLE_S32LE
:
280 case PA_SAMPLE_S32BE
:
281 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
282 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
293 static pa_sample_format_t
pa_resampler_choose_work_format(
294 pa_resample_method_t method
,
295 pa_sample_format_t a
,
296 pa_sample_format_t b
,
298 pa_sample_format_t work_format
;
300 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
301 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
302 pa_assert(method
>= 0);
303 pa_assert(method
< PA_RESAMPLER_MAX
);
305 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
306 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
309 /* This block is for resampling functions that only
310 * support the S16 sample format. */
311 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
312 case PA_RESAMPLER_FFMPEG
:
313 work_format
= PA_SAMPLE_S16NE
;
316 /* This block is for resampling functions that support
317 * any sample format. */
318 case PA_RESAMPLER_COPY
: /* fall through */
319 case PA_RESAMPLER_TRIVIAL
:
320 if (!map_required
&& a
== b
) {
324 /* Else fall trough */
325 case PA_RESAMPLER_PEAKS
:
326 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
327 work_format
= PA_SAMPLE_S16NE
;
328 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
329 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
330 work_format
= PA_SAMPLE_FLOAT32NE
;
332 work_format
= PA_SAMPLE_S16NE
;
336 work_format
= PA_SAMPLE_FLOAT32NE
;
342 pa_resampler
* pa_resampler_new(
344 const pa_sample_spec
*a
,
345 const pa_channel_map
*am
,
346 const pa_sample_spec
*b
,
347 const pa_channel_map
*bm
,
348 pa_resample_method_t method
,
349 pa_resample_flags_t flags
) {
351 pa_resampler
*r
= NULL
;
356 pa_assert(pa_sample_spec_valid(a
));
357 pa_assert(pa_sample_spec_valid(b
));
358 pa_assert(method
>= 0);
359 pa_assert(method
< PA_RESAMPLER_MAX
);
361 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
363 r
= pa_xnew0(pa_resampler
, 1);
368 /* Fill sample specs */
372 /* set up the remap structure */
373 r
->remap
.i_ss
= &r
->i_ss
;
374 r
->remap
.o_ss
= &r
->o_ss
;
375 r
->remap
.format
= &r
->work_format
;
379 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
384 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
387 r
->i_fz
= pa_frame_size(a
);
388 r
->o_fz
= pa_frame_size(b
);
392 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
394 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
396 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
398 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
400 if (r
->i_ss
.format
!= r
->work_format
) {
401 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
402 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
405 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
406 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
411 if (r
->o_ss
.format
!= r
->work_format
) {
412 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
413 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
416 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
417 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
422 /* initialize implementation */
423 if (init_table
[method
](r
) < 0)
434 void pa_resampler_free(pa_resampler
*r
) {
440 pa_xfree(r
->impl
.data
);
442 if (r
->to_work_format_buf
.memblock
)
443 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
444 if (r
->remap_buf
.memblock
)
445 pa_memblock_unref(r
->remap_buf
.memblock
);
446 if (r
->resample_buf
.memblock
)
447 pa_memblock_unref(r
->resample_buf
.memblock
);
448 if (r
->from_work_format_buf
.memblock
)
449 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
454 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
457 pa_assert(r
->impl
.update_rates
);
459 if (r
->i_ss
.rate
== rate
)
464 r
->impl
.update_rates(r
);
467 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
470 pa_assert(r
->impl
.update_rates
);
472 if (r
->o_ss
.rate
== rate
)
477 r
->impl
.update_rates(r
);
480 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
483 /* Let's round up here to make it more likely that the caller will get at
484 * least out_length amount of data from pa_resampler_run().
486 * We don't take the leftover into account here. If we did, then it might
487 * be in theory possible that this function would return 0 and
488 * pa_resampler_run() would also return 0. That could lead to infinite
489 * loops. When the leftover is ignored here, such loops would eventually
490 * terminate, because the leftover would grow each round, finally
491 * surpassing the minimum input threshold of the resampler. */
492 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
;
495 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
500 /* Let's round up here to ensure that the caller will always allocate big
501 * enough output buffer. */
503 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
505 if (r
->remap_buf_contains_leftover_data
)
506 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
508 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
511 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
512 size_t block_size_max
;
513 pa_sample_spec max_ss
;
519 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
521 /* We deduce the "largest" sample spec we're using during the
523 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
525 /* We silently assume that the format enum is ordered by size */
526 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
527 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
529 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
531 max_fs
= pa_frame_size(&max_ss
);
532 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
534 if (r
->remap_buf_contains_leftover_data
)
535 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
537 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
540 void pa_resampler_reset(pa_resampler
*r
) {
546 r
->remap_buf_contains_leftover_data
= false;
549 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
555 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
561 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
567 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
573 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
579 static const char * const resample_methods
[] = {
580 "src-sinc-best-quality",
581 "src-sinc-medium-quality",
583 "src-zero-order-hold",
614 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
616 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
619 return resample_methods
[m
];
622 int pa_resample_method_supported(pa_resample_method_t m
) {
624 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
627 #ifndef HAVE_LIBSAMPLERATE
628 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
633 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
635 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
642 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
643 pa_resample_method_t m
;
647 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
648 if (pa_streq(string
, resample_methods
[m
]))
651 if (pa_streq(string
, "speex-fixed"))
652 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
654 if (pa_streq(string
, "speex-float"))
655 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
657 return PA_RESAMPLER_INVALID
;
660 static bool on_left(pa_channel_position_t p
) {
663 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
664 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
665 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
666 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
667 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
668 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
671 static bool on_right(pa_channel_position_t p
) {
674 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
675 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
676 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
677 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
678 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
679 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
682 static bool on_center(pa_channel_position_t p
) {
685 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
686 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
687 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
688 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
689 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
692 static bool on_lfe(pa_channel_position_t p
) {
694 p
== PA_CHANNEL_POSITION_LFE
;
697 static bool on_front(pa_channel_position_t p
) {
699 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
700 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
701 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
702 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
703 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
704 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
705 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
706 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
709 static bool on_rear(pa_channel_position_t p
) {
711 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
712 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
713 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
714 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
715 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
716 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
719 static bool on_side(pa_channel_position_t p
) {
721 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
722 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
723 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
733 static int front_rear_side(pa_channel_position_t p
) {
743 static void calc_map_table(pa_resampler
*r
) {
746 bool ic_connected
[PA_CHANNELS_MAX
];
754 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
)))))
759 n_oc
= r
->o_ss
.channels
;
760 n_ic
= r
->i_ss
.channels
;
762 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
763 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
765 memset(ic_connected
, 0, sizeof(ic_connected
));
766 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
768 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
771 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
772 m
->map_table_f
[oc
][oc
] = 1.0f
;
774 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
776 for (oc
= 0; oc
< n_oc
; oc
++) {
777 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
779 for (ic
= 0; ic
< n_ic
; ic
++) {
780 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
782 /* We shall not do any remixing. Hence, just check by name */
784 m
->map_table_f
[oc
][ic
] = 1.0f
;
789 /* OK, we shall do the full monty: upmixing and downmixing. Our
790 * algorithm is relatively simple, does not do spacialization, delay
791 * elements or apply lowpass filters for LFE. Patches are always
792 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
793 * probably wouldn't make any sense anyway.)
795 * This code is not idempotent: downmixing an upmixed stereo stream is
796 * not identical to the original. The volume will not match, and the
797 * two channels will be a linear combination of both.
799 * This is loosely based on random suggestions found on the Internet,
801 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
804 * The algorithm works basically like this:
806 * 1) Connect all channels with matching names.
809 * S:Mono: Copy into all D:channels
810 * D:Mono: Avg all S:channels
812 * 3) Mix D:Left, D:Right:
813 * D:Left: If not connected, avg all S:Left
814 * D:Right: If not connected, avg all S:Right
817 * If not connected, avg all S:Center
818 * If still not connected, avg all S:Left, S:Right
821 * If not connected, avg all S:*
823 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
824 * connected, mix into all D:left and all D:right channels. Gain is
827 * 7) Make sure S:Center, S:LFE is used:
829 * S:Center, S:LFE: If not connected, mix into all D:left, all
830 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
831 * for LFE. C-front is only mixed into L-front/R-front if available,
832 * otherwise into all L/R channels. Similarly for C-rear.
834 * 8) Normalize each row in the matrix such that the sum for each row is
835 * not larger than 1.0 in order to avoid clipping.
837 * S: and D: shall relate to the source resp. destination channels.
839 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
840 * rear if needed. For 4: we try to find some suitable C source for C,
841 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
842 * channels. For 6: the rear channels should not be dropped entirely,
843 * however have only minimal impact. For 7: movies usually encode
844 * speech on the center channel. Thus we have to make sure this channel
845 * is distributed to L and R if not available in the output. Also, LFE
846 * is used to achieve a greater dynamic range, and thus we should try
847 * to do our best to pass it to L+R.
854 ic_unconnected_left
= 0,
855 ic_unconnected_right
= 0,
856 ic_unconnected_center
= 0,
857 ic_unconnected_lfe
= 0;
858 bool ic_unconnected_center_mixed_in
= 0;
862 for (ic
= 0; ic
< n_ic
; ic
++) {
863 if (on_left(r
->i_cm
.map
[ic
]))
865 if (on_right(r
->i_cm
.map
[ic
]))
867 if (on_center(r
->i_cm
.map
[ic
]))
871 for (oc
= 0; oc
< n_oc
; oc
++) {
872 bool oc_connected
= false;
873 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
875 for (ic
= 0; ic
< n_ic
; ic
++) {
876 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
878 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
879 m
->map_table_f
[oc
][ic
] = 1.0f
;
882 ic_connected
[ic
] = true;
884 else if (b
== PA_CHANNEL_POSITION_MONO
) {
885 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
888 ic_connected
[ic
] = true;
893 /* Try to find matching input ports for this output port */
897 /* We are not connected and on the left side, let's
898 * average all left side input channels. */
901 for (ic
= 0; ic
< n_ic
; ic
++)
902 if (on_left(r
->i_cm
.map
[ic
])) {
903 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
904 ic_connected
[ic
] = true;
907 /* We ignore the case where there is no left input channel.
908 * Something is really wrong in this case anyway. */
910 } else if (on_right(b
)) {
912 /* We are not connected and on the right side, let's
913 * average all right side input channels. */
916 for (ic
= 0; ic
< n_ic
; ic
++)
917 if (on_right(r
->i_cm
.map
[ic
])) {
918 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
919 ic_connected
[ic
] = true;
922 /* We ignore the case where there is no right input
923 * channel. Something is really wrong in this case anyway.
926 } else if (on_center(b
)) {
930 /* We are not connected and at the center. Let's average
931 * all center input channels. */
933 for (ic
= 0; ic
< n_ic
; ic
++)
934 if (on_center(r
->i_cm
.map
[ic
])) {
935 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
936 ic_connected
[ic
] = true;
939 } else if (ic_left
+ ic_right
> 0) {
941 /* Hmm, no center channel around, let's synthesize it
942 * by mixing L and R.*/
944 for (ic
= 0; ic
< n_ic
; ic
++)
945 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
946 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
947 ic_connected
[ic
] = true;
951 /* We ignore the case where there is not even a left or
952 * right input channel. Something is really wrong in this
955 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
957 /* We are not connected and an LFE. Let's average all
958 * channels for LFE. */
960 for (ic
= 0; ic
< n_ic
; ic
++)
961 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
963 /* Please note that a channel connected to LFE doesn't
964 * really count as connected. */
969 for (ic
= 0; ic
< n_ic
; ic
++) {
970 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
972 if (ic_connected
[ic
])
976 ic_unconnected_left
++;
977 else if (on_right(a
))
978 ic_unconnected_right
++;
979 else if (on_center(a
))
980 ic_unconnected_center
++;
982 ic_unconnected_lfe
++;
985 for (ic
= 0; ic
< n_ic
; ic
++) {
986 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
988 if (ic_connected
[ic
])
991 for (oc
= 0; oc
< n_oc
; oc
++) {
992 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
994 if (on_left(a
) && on_left(b
))
995 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
997 else if (on_right(a
) && on_right(b
))
998 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1000 else if (on_center(a
) && on_center(b
)) {
1001 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1002 ic_unconnected_center_mixed_in
= true;
1004 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1005 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1009 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1010 unsigned ncenter
[PA_CHANNELS_MAX
];
1011 bool found_frs
[PA_CHANNELS_MAX
];
1013 memset(ncenter
, 0, sizeof(ncenter
));
1014 memset(found_frs
, 0, sizeof(found_frs
));
1016 /* Hmm, as it appears there was no center channel we
1017 could mix our center channel in. In this case, mix it into
1018 left and right. Using .5 as the factor. */
1020 for (ic
= 0; ic
< n_ic
; ic
++) {
1022 if (ic_connected
[ic
])
1025 if (!on_center(r
->i_cm
.map
[ic
]))
1028 for (oc
= 0; oc
< n_oc
; oc
++) {
1030 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1033 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1034 found_frs
[ic
] = true;
1039 for (oc
= 0; oc
< n_oc
; oc
++) {
1041 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1044 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1049 for (oc
= 0; oc
< n_oc
; oc
++) {
1051 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1054 if (ncenter
[oc
] <= 0)
1057 for (ic
= 0; ic
< n_ic
; ic
++) {
1059 if (!on_center(r
->i_cm
.map
[ic
]))
1062 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1063 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1069 for (oc
= 0; oc
< n_oc
; oc
++) {
1071 for (ic
= 0; ic
< n_ic
; ic
++)
1072 sum
+= m
->map_table_f
[oc
][ic
];
1075 for (ic
= 0; ic
< n_ic
; ic
++)
1076 m
->map_table_f
[oc
][ic
] /= sum
;
1079 /* make an 16:16 int version of the matrix */
1080 for (oc
= 0; oc
< n_oc
; oc
++)
1081 for (ic
= 0; ic
< n_ic
; ic
++)
1082 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1084 s
= pa_strbuf_new();
1086 pa_strbuf_printf(s
, " ");
1087 for (ic
= 0; ic
< n_ic
; ic
++)
1088 pa_strbuf_printf(s
, " I%02u ", ic
);
1089 pa_strbuf_puts(s
, "\n +");
1091 for (ic
= 0; ic
< n_ic
; ic
++)
1092 pa_strbuf_printf(s
, "------");
1093 pa_strbuf_puts(s
, "\n");
1095 for (oc
= 0; oc
< n_oc
; oc
++) {
1096 pa_strbuf_printf(s
, "O%02u |", oc
);
1098 for (ic
= 0; ic
< n_ic
; ic
++)
1099 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1101 pa_strbuf_puts(s
, "\n");
1104 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1107 /* initialize the remapping function */
1111 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1117 pa_assert(input
->memblock
);
1119 /* Convert the incoming sample into the work sample format and place them
1120 * in to_work_format_buf. */
1122 if (!r
->to_work_format_func
|| !input
->length
)
1125 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1127 r
->to_work_format_buf
.index
= 0;
1128 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1130 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1131 if (r
->to_work_format_buf
.memblock
)
1132 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1134 r
->to_work_format_buf_samples
= n_samples
;
1135 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1138 src
= pa_memblock_acquire_chunk(input
);
1139 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1141 r
->to_work_format_func(n_samples
, src
, dst
);
1143 pa_memblock_release(input
->memblock
);
1144 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1146 return &r
->to_work_format_buf
;
1149 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1150 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1152 size_t leftover_length
= 0;
1157 pa_assert(input
->memblock
);
1159 /* Remap channels and place the result in remap_buf. There may be leftover
1160 * data in the beginning of remap_buf. The leftover data is already
1161 * remapped, so it's not part of the input, it's part of the output. */
1163 have_leftover
= r
->remap_buf_contains_leftover_data
;
1164 r
->remap_buf_contains_leftover_data
= false;
1166 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1168 else if (input
->length
<= 0)
1169 return &r
->remap_buf
;
1171 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1172 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1174 if (have_leftover
) {
1175 leftover_length
= r
->remap_buf
.length
;
1176 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1179 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1180 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1182 if (have_leftover
) {
1183 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1184 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1186 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1187 dst
= pa_memblock_acquire(new_block
);
1188 memcpy(dst
, src
, leftover_length
);
1189 pa_memblock_release(r
->remap_buf
.memblock
);
1190 pa_memblock_release(new_block
);
1192 pa_memblock_unref(r
->remap_buf
.memblock
);
1193 r
->remap_buf
.memblock
= new_block
;
1194 r
->remap_buf_size
= r
->remap_buf
.length
;
1198 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1199 if (r
->remap_buf
.memblock
)
1200 pa_memblock_unref(r
->remap_buf
.memblock
);
1202 r
->remap_buf_size
= r
->remap_buf
.length
;
1203 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1207 src
= pa_memblock_acquire_chunk(input
);
1208 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1210 if (r
->map_required
) {
1211 pa_remap_t
*remap
= &r
->remap
;
1213 pa_assert(remap
->do_remap
);
1214 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1217 memcpy(dst
, src
, input
->length
);
1219 pa_memblock_release(input
->memblock
);
1220 pa_memblock_release(r
->remap_buf
.memblock
);
1222 return &r
->remap_buf
;
1225 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1226 unsigned in_n_frames
, in_n_samples
;
1227 unsigned out_n_frames
, out_n_samples
;
1232 /* Resample the data and place the result in resample_buf. */
1234 if (!r
->impl
.resample
|| !input
->length
)
1237 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1238 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1240 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1241 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1243 r
->resample_buf
.index
= 0;
1244 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1246 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1247 if (r
->resample_buf
.memblock
)
1248 pa_memblock_unref(r
->resample_buf
.memblock
);
1250 r
->resample_buf_samples
= out_n_samples
;
1251 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1254 r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1255 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1257 return &r
->resample_buf
;
1260 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1261 unsigned n_samples
, n_frames
;
1267 /* Convert the data into the correct sample type and place the result in
1268 * from_work_format_buf. */
1270 if (!r
->from_work_format_func
|| !input
->length
)
1273 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1274 n_frames
= n_samples
/ r
->o_ss
.channels
;
1276 r
->from_work_format_buf
.index
= 0;
1277 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1279 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1280 if (r
->from_work_format_buf
.memblock
)
1281 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1283 r
->from_work_format_buf_samples
= n_samples
;
1284 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1287 src
= pa_memblock_acquire_chunk(input
);
1288 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1289 r
->from_work_format_func(n_samples
, src
, dst
);
1290 pa_memblock_release(input
->memblock
);
1291 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1293 return &r
->from_work_format_buf
;
1296 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1302 pa_assert(in
->length
);
1303 pa_assert(in
->memblock
);
1304 pa_assert(in
->length
% r
->i_fz
== 0);
1306 buf
= (pa_memchunk
*) in
;
1307 buf
= convert_to_work_format(r
, buf
);
1308 buf
= remap_channels(r
, buf
);
1309 buf
= resample(r
, buf
);
1312 buf
= convert_from_work_format(r
, buf
);
1316 pa_memblock_ref(buf
->memblock
);
1318 pa_memchunk_reset(buf
);
1320 pa_memchunk_reset(out
);
1323 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1330 /* Store the leftover to remap_buf. */
1332 r
->remap_buf
.length
= len
;
1334 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1335 if (r
->remap_buf
.memblock
)
1336 pa_memblock_unref(r
->remap_buf
.memblock
);
1338 r
->remap_buf_size
= r
->remap_buf
.length
;
1339 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1342 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1343 memcpy(dst
, buf
, r
->remap_buf
.length
);
1344 pa_memblock_release(r
->remap_buf
.memblock
);
1346 r
->remap_buf_contains_leftover_data
= true;
1349 /*** libsamplerate based implementation ***/
1351 #ifdef HAVE_LIBSAMPLERATE
1352 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1359 pa_assert(out_n_frames
);
1361 state
= r
->impl
.data
;
1362 memset(&data
, 0, sizeof(data
));
1364 data
.data_in
= pa_memblock_acquire_chunk(input
);
1365 data
.input_frames
= (long int) in_n_frames
;
1367 data
.data_out
= pa_memblock_acquire_chunk(output
);
1368 data
.output_frames
= (long int) *out_n_frames
;
1370 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1371 data
.end_of_input
= 0;
1373 pa_assert_se(src_process(state
, &data
) == 0);
1375 if (data
.input_frames_used
< in_n_frames
) {
1376 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1377 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1379 save_leftover(r
, leftover_data
, leftover_length
);
1382 pa_memblock_release(input
->memblock
);
1383 pa_memblock_release(output
->memblock
);
1385 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1388 static void libsamplerate_update_rates(pa_resampler
*r
) {
1392 state
= r
->impl
.data
;
1393 pa_assert_se(src_set_ratio(state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1396 static void libsamplerate_reset(pa_resampler
*r
) {
1400 state
= r
->impl
.data
;
1401 pa_assert_se(src_reset(state
) == 0);
1404 static void libsamplerate_free(pa_resampler
*r
) {
1408 state
= r
->impl
.data
;
1413 static int libsamplerate_init(pa_resampler
*r
) {
1419 if (!(state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1422 r
->impl
.free
= libsamplerate_free
;
1423 r
->impl
.update_rates
= libsamplerate_update_rates
;
1424 r
->impl
.resample
= libsamplerate_resample
;
1425 r
->impl
.reset
= libsamplerate_reset
;
1426 r
->impl
.data
= state
;
1433 /*** speex based implementation ***/
1435 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1437 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1438 SpeexResamplerState
*state
;
1443 pa_assert(out_n_frames
);
1445 state
= r
->impl
.data
;
1447 in
= pa_memblock_acquire_chunk(input
);
1448 out
= pa_memblock_acquire_chunk(output
);
1450 pa_assert_se(speex_resampler_process_interleaved_float(state
, in
, &inf
, out
, &outf
) == 0);
1452 pa_memblock_release(input
->memblock
);
1453 pa_memblock_release(output
->memblock
);
1455 pa_assert(inf
== in_n_frames
);
1456 *out_n_frames
= outf
;
1459 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1461 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1462 SpeexResamplerState
*state
;
1467 pa_assert(out_n_frames
);
1469 state
= r
->impl
.data
;
1471 in
= pa_memblock_acquire_chunk(input
);
1472 out
= pa_memblock_acquire_chunk(output
);
1474 pa_assert_se(speex_resampler_process_interleaved_int(state
, in
, &inf
, out
, &outf
) == 0);
1476 pa_memblock_release(input
->memblock
);
1477 pa_memblock_release(output
->memblock
);
1479 pa_assert(inf
== in_n_frames
);
1480 *out_n_frames
= outf
;
1483 static void speex_update_rates(pa_resampler
*r
) {
1484 SpeexResamplerState
*state
;
1487 state
= r
->impl
.data
;
1489 pa_assert_se(speex_resampler_set_rate(state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1492 static void speex_reset(pa_resampler
*r
) {
1493 SpeexResamplerState
*state
;
1496 state
= r
->impl
.data
;
1498 pa_assert_se(speex_resampler_reset_mem(state
) == 0);
1501 static void speex_free(pa_resampler
*r
) {
1502 SpeexResamplerState
*state
;
1505 state
= r
->impl
.data
;
1509 speex_resampler_destroy(state
);
1512 static int speex_init(pa_resampler
*r
) {
1514 SpeexResamplerState
*state
;
1518 r
->impl
.free
= speex_free
;
1519 r
->impl
.update_rates
= speex_update_rates
;
1520 r
->impl
.reset
= speex_reset
;
1522 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1524 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1525 r
->impl
.resample
= speex_resample_int
;
1528 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1530 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1531 r
->impl
.resample
= speex_resample_float
;
1534 pa_log_info("Choosing speex quality setting %i.", q
);
1536 if (!(state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1539 r
->impl
.data
= state
;
1545 /* Trivial implementation */
1547 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1549 unsigned i_index
, o_index
;
1551 struct trivial_data
*trivial_data
;
1556 pa_assert(out_n_frames
);
1558 trivial_data
= r
->impl
.data
;
1559 fz
= r
->w_sz
* r
->o_ss
.channels
;
1561 src
= pa_memblock_acquire_chunk(input
);
1562 dst
= pa_memblock_acquire_chunk(output
);
1564 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1565 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1566 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1568 if (i_index
>= in_n_frames
)
1571 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1573 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1576 pa_memblock_release(input
->memblock
);
1577 pa_memblock_release(output
->memblock
);
1579 *out_n_frames
= o_index
;
1581 trivial_data
->i_counter
+= in_n_frames
;
1583 /* Normalize counters */
1584 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1585 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1587 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1588 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1592 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1593 struct trivial_data
*trivial_data
;
1596 trivial_data
= r
->impl
.data
;
1598 trivial_data
->i_counter
= 0;
1599 trivial_data
->o_counter
= 0;
1602 static int trivial_init(pa_resampler
*r
) {
1603 struct trivial_data
*trivial_data
;
1606 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1608 r
->impl
.resample
= trivial_resample
;
1609 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1610 r
->impl
.reset
= trivial_update_rates_or_reset
;
1611 r
->impl
.data
= trivial_data
;
1616 /* Peak finder implementation */
1618 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1619 unsigned c
, o_index
= 0;
1620 unsigned i
, i_end
= 0;
1622 struct peaks_data
*peaks_data
;
1627 pa_assert(out_n_frames
);
1629 peaks_data
= r
->impl
.data
;
1630 src
= pa_memblock_acquire_chunk(input
);
1631 dst
= pa_memblock_acquire_chunk(output
);
1633 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1634 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1636 while (i_end
< in_n_frames
) {
1637 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1638 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1640 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1642 /* 1ch float is treated separately, because that is the common case */
1643 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1644 float *s
= (float*) src
+ i
;
1645 float *d
= (float*) dst
+ o_index
;
1647 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1648 float n
= fabsf(*s
++);
1650 if (n
> peaks_data
->max_f
[0])
1651 peaks_data
->max_f
[0] = n
;
1655 *d
= peaks_data
->max_f
[0];
1656 peaks_data
->max_f
[0] = 0;
1657 o_index
++, peaks_data
->o_counter
++;
1659 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1660 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1661 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1663 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1664 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1665 int16_t n
= abs(*s
++);
1667 if (n
> peaks_data
->max_i
[c
])
1668 peaks_data
->max_i
[c
] = n
;
1672 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1673 *d
= peaks_data
->max_i
[c
];
1674 peaks_data
->max_i
[c
] = 0;
1676 o_index
++, peaks_data
->o_counter
++;
1679 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1680 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1682 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1683 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1684 float n
= fabsf(*s
++);
1686 if (n
> peaks_data
->max_f
[c
])
1687 peaks_data
->max_f
[c
] = n
;
1691 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1692 *d
= peaks_data
->max_f
[c
];
1693 peaks_data
->max_f
[c
] = 0;
1695 o_index
++, peaks_data
->o_counter
++;
1700 pa_memblock_release(input
->memblock
);
1701 pa_memblock_release(output
->memblock
);
1703 *out_n_frames
= o_index
;
1705 peaks_data
->i_counter
+= in_n_frames
;
1707 /* Normalize counters */
1708 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1709 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1711 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1712 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1716 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1717 struct peaks_data
*peaks_data
;
1720 peaks_data
= r
->impl
.data
;
1722 peaks_data
->i_counter
= 0;
1723 peaks_data
->o_counter
= 0;
1726 static int peaks_init(pa_resampler
*r
) {
1727 struct peaks_data
*peaks_data
;
1729 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1730 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1732 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1734 r
->impl
.resample
= peaks_resample
;
1735 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1736 r
->impl
.reset
= peaks_update_rates_or_reset
;
1737 r
->impl
.data
= peaks_data
;
1742 /*** ffmpeg based implementation ***/
1744 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1745 unsigned used_frames
= 0, c
;
1746 int previous_consumed_frames
= -1;
1747 struct ffmpeg_data
*ffmpeg_data
;
1752 pa_assert(out_n_frames
);
1754 ffmpeg_data
= r
->impl
.data
;
1756 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1759 int16_t *p
, *t
, *k
, *q
, *s
;
1760 int consumed_frames
;
1762 /* Allocate a new block */
1763 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1764 p
= pa_memblock_acquire(b
);
1766 /* Now copy the input data, splitting up channels */
1767 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1769 for (u
= 0; u
< in_n_frames
; u
++) {
1771 t
+= r
->o_ss
.channels
;
1774 pa_memblock_release(input
->memblock
);
1776 /* Allocate buffer for the result */
1777 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1778 q
= pa_memblock_acquire(w
);
1781 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1784 (int) in_n_frames
, (int) *out_n_frames
,
1785 c
>= (unsigned) (r
->o_ss
.channels
-1));
1787 pa_memblock_release(b
);
1788 pa_memblock_unref(b
);
1790 pa_assert(consumed_frames
<= (int) in_n_frames
);
1791 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1792 previous_consumed_frames
= consumed_frames
;
1794 /* And place the results in the output buffer */
1795 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1796 for (u
= 0; u
< used_frames
; u
++) {
1799 s
+= r
->o_ss
.channels
;
1801 pa_memblock_release(output
->memblock
);
1802 pa_memblock_release(w
);
1803 pa_memblock_unref(w
);
1806 if (previous_consumed_frames
< (int) in_n_frames
) {
1807 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1808 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1810 save_leftover(r
, leftover_data
, leftover_length
);
1811 pa_memblock_release(input
->memblock
);
1814 *out_n_frames
= used_frames
;
1817 static void ffmpeg_free(pa_resampler
*r
) {
1819 struct ffmpeg_data
*ffmpeg_data
;
1823 ffmpeg_data
= r
->impl
.data
;
1824 if (ffmpeg_data
->state
)
1825 av_resample_close(ffmpeg_data
->state
);
1827 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1828 if (ffmpeg_data
->buf
[c
].memblock
)
1829 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1832 static int ffmpeg_init(pa_resampler
*r
) {
1834 struct ffmpeg_data
*ffmpeg_data
;
1838 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1840 /* We could probably implement different quality levels by
1841 * adjusting the filter parameters here. However, ffmpeg
1842 * internally only uses these hardcoded values, so let's use them
1843 * here for now as well until ffmpeg makes this configurable. */
1845 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1848 r
->impl
.free
= ffmpeg_free
;
1849 r
->impl
.resample
= ffmpeg_resample
;
1850 r
->impl
.data
= (void *) ffmpeg_data
;
1852 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1853 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1858 /*** copy (noop) implementation ***/
1860 static int copy_init(pa_resampler
*r
) {
1863 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);