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 void (*impl_free
)(pa_resampler
*r
);
78 void (*impl_update_rates
)(pa_resampler
*r
);
79 void (*impl_resample
)(pa_resampler
*r
, const pa_memchunk
*in
, unsigned in_samples
, pa_memchunk
*out
, unsigned *out_samples
);
80 void (*impl_reset
)(pa_resampler
*r
);
84 struct trivial_data
{ /* data specific to the trivial resampler */
89 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
93 float max_f
[PA_CHANNELS_MAX
];
94 int16_t max_i
[PA_CHANNELS_MAX
];
97 #ifdef HAVE_LIBSAMPLERATE
98 struct src_data
{ /* data specific to libsamplerate */
104 struct speex_data
{ /* data specific to speex */
105 SpeexResamplerState
* state
;
109 struct ffmpeg_data
{ /* data specific to ffmpeg */
110 struct AVResampleContext
*state
;
111 pa_memchunk buf
[PA_CHANNELS_MAX
];
114 static int copy_init(pa_resampler
*r
);
115 static int trivial_init(pa_resampler
*r
);
117 static int speex_init(pa_resampler
*r
);
119 static int ffmpeg_init(pa_resampler
*r
);
120 static int peaks_init(pa_resampler
*r
);
121 #ifdef HAVE_LIBSAMPLERATE
122 static int libsamplerate_init(pa_resampler
*r
);
125 static void calc_map_table(pa_resampler
*r
);
127 static int (* const init_table
[])(pa_resampler
*r
) = {
128 #ifdef HAVE_LIBSAMPLERATE
129 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
130 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
131 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
132 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
133 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
135 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
136 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
137 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
138 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
139 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
141 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
176 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
187 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
189 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
190 [PA_RESAMPLER_AUTO
] = NULL
,
191 [PA_RESAMPLER_COPY
] = copy_init
,
192 [PA_RESAMPLER_PEAKS
] = peaks_init
,
195 static pa_resample_method_t
pa_resampler_fix_method(
196 pa_resample_flags_t flags
,
197 pa_resample_method_t method
,
198 const uint32_t rate_a
,
199 const uint32_t rate_b
) {
201 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
202 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
203 pa_assert(method
>= 0);
204 pa_assert(method
< PA_RESAMPLER_MAX
);
206 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
207 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
208 method
= PA_RESAMPLER_COPY
;
211 if (!pa_resample_method_supported(method
)) {
212 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
213 method
= PA_RESAMPLER_AUTO
;
217 case PA_RESAMPLER_COPY
:
218 if (rate_a
!= rate_b
) {
219 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
222 /* Else fall through */
223 case PA_RESAMPLER_FFMPEG
:
224 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
225 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
226 method
= PA_RESAMPLER_AUTO
;
233 if (method
== PA_RESAMPLER_AUTO
) {
235 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
237 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
238 method
= PA_RESAMPLER_TRIVIAL
;
240 method
= PA_RESAMPLER_FFMPEG
;
247 /* Return true if a is a more precise sample format than b, else return false */
248 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
249 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
250 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
259 case PA_SAMPLE_S16LE
:
260 case PA_SAMPLE_S16BE
:
261 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
267 case PA_SAMPLE_S24LE
:
268 case PA_SAMPLE_S24BE
:
269 case PA_SAMPLE_S24_32LE
:
270 case PA_SAMPLE_S24_32BE
:
271 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
272 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
278 case PA_SAMPLE_FLOAT32LE
:
279 case PA_SAMPLE_FLOAT32BE
:
280 case PA_SAMPLE_S32LE
:
281 case PA_SAMPLE_S32BE
:
282 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
283 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
294 static pa_sample_format_t
pa_resampler_choose_work_format(
295 pa_resample_method_t method
,
296 pa_sample_format_t a
,
297 pa_sample_format_t b
,
299 pa_sample_format_t work_format
;
301 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
302 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
303 pa_assert(method
>= 0);
304 pa_assert(method
< PA_RESAMPLER_MAX
);
306 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
307 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
310 /* This block is for resampling functions that only
311 * support the S16 sample format. */
312 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
313 case PA_RESAMPLER_FFMPEG
:
314 work_format
= PA_SAMPLE_S16NE
;
317 /* This block is for resampling functions that support
318 * any sample format. */
319 case PA_RESAMPLER_COPY
: /* fall through */
320 case PA_RESAMPLER_TRIVIAL
:
321 if (!map_required
&& a
== b
) {
325 /* Else fall trough */
326 case PA_RESAMPLER_PEAKS
:
327 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
328 work_format
= PA_SAMPLE_S16NE
;
329 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
330 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
331 work_format
= PA_SAMPLE_FLOAT32NE
;
333 work_format
= PA_SAMPLE_S16NE
;
337 work_format
= PA_SAMPLE_FLOAT32NE
;
343 pa_resampler
* pa_resampler_new(
345 const pa_sample_spec
*a
,
346 const pa_channel_map
*am
,
347 const pa_sample_spec
*b
,
348 const pa_channel_map
*bm
,
349 pa_resample_method_t method
,
350 pa_resample_flags_t flags
) {
352 pa_resampler
*r
= NULL
;
357 pa_assert(pa_sample_spec_valid(a
));
358 pa_assert(pa_sample_spec_valid(b
));
359 pa_assert(method
>= 0);
360 pa_assert(method
< PA_RESAMPLER_MAX
);
362 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
364 r
= pa_xnew0(pa_resampler
, 1);
369 /* Fill sample specs */
373 /* set up the remap structure */
374 r
->remap
.i_ss
= &r
->i_ss
;
375 r
->remap
.o_ss
= &r
->o_ss
;
376 r
->remap
.format
= &r
->work_format
;
380 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
385 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
388 r
->i_fz
= pa_frame_size(a
);
389 r
->o_fz
= pa_frame_size(b
);
393 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
395 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
397 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
399 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
401 if (r
->i_ss
.format
!= r
->work_format
) {
402 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
403 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
406 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
407 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
412 if (r
->o_ss
.format
!= r
->work_format
) {
413 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
414 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
417 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
418 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
423 /* initialize implementation */
424 if (init_table
[method
](r
) < 0)
435 void pa_resampler_free(pa_resampler
*r
) {
441 if (r
->to_work_format_buf
.memblock
)
442 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
443 if (r
->remap_buf
.memblock
)
444 pa_memblock_unref(r
->remap_buf
.memblock
);
445 if (r
->resample_buf
.memblock
)
446 pa_memblock_unref(r
->resample_buf
.memblock
);
447 if (r
->from_work_format_buf
.memblock
)
448 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
450 pa_xfree(r
->impl_data
);
454 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
458 if (r
->i_ss
.rate
== rate
)
463 r
->impl_update_rates(r
);
466 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
470 if (r
->o_ss
.rate
== rate
)
475 r
->impl_update_rates(r
);
478 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
481 /* Let's round up here to make it more likely that the caller will get at
482 * least out_length amount of data from pa_resampler_run().
484 * We don't take the leftover into account here. If we did, then it might
485 * be in theory possible that this function would return 0 and
486 * pa_resampler_run() would also return 0. That could lead to infinite
487 * loops. When the leftover is ignored here, such loops would eventually
488 * terminate, because the leftover would grow each round, finally
489 * surpassing the minimum input threshold of the resampler. */
490 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
;
493 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
498 /* Let's round up here to ensure that the caller will always allocate big
499 * enough output buffer. */
501 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
503 if (r
->remap_buf_contains_leftover_data
)
504 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
506 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
509 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
510 size_t block_size_max
;
511 pa_sample_spec max_ss
;
517 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
519 /* We deduce the "largest" sample spec we're using during the
521 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
523 /* We silently assume that the format enum is ordered by size */
524 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
525 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
527 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
529 max_fs
= pa_frame_size(&max_ss
);
530 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
532 if (r
->remap_buf_contains_leftover_data
)
533 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
535 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
538 void pa_resampler_reset(pa_resampler
*r
) {
544 r
->remap_buf_contains_leftover_data
= false;
547 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
553 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
559 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
565 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
571 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
577 static const char * const resample_methods
[] = {
578 "src-sinc-best-quality",
579 "src-sinc-medium-quality",
581 "src-zero-order-hold",
612 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
614 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
617 return resample_methods
[m
];
620 int pa_resample_method_supported(pa_resample_method_t m
) {
622 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
625 #ifndef HAVE_LIBSAMPLERATE
626 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
631 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
633 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
640 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
641 pa_resample_method_t m
;
645 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
646 if (pa_streq(string
, resample_methods
[m
]))
649 if (pa_streq(string
, "speex-fixed"))
650 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
652 if (pa_streq(string
, "speex-float"))
653 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
655 return PA_RESAMPLER_INVALID
;
658 static bool on_left(pa_channel_position_t p
) {
661 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
662 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
663 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
664 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
665 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
666 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
669 static bool on_right(pa_channel_position_t p
) {
672 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
673 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
674 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
675 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
676 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
677 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
680 static bool on_center(pa_channel_position_t p
) {
683 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
684 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
685 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
686 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
687 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
690 static bool on_lfe(pa_channel_position_t p
) {
692 p
== PA_CHANNEL_POSITION_LFE
;
695 static bool on_front(pa_channel_position_t p
) {
697 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
698 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
699 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
700 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
701 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
702 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
703 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
704 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
707 static bool on_rear(pa_channel_position_t p
) {
709 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
710 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
711 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
712 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
713 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
714 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
717 static bool on_side(pa_channel_position_t p
) {
719 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
720 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
721 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
731 static int front_rear_side(pa_channel_position_t p
) {
741 static void calc_map_table(pa_resampler
*r
) {
744 bool ic_connected
[PA_CHANNELS_MAX
];
752 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
)))))
757 n_oc
= r
->o_ss
.channels
;
758 n_ic
= r
->i_ss
.channels
;
760 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
761 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
763 memset(ic_connected
, 0, sizeof(ic_connected
));
764 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
766 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
769 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
770 m
->map_table_f
[oc
][oc
] = 1.0f
;
772 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
774 for (oc
= 0; oc
< n_oc
; oc
++) {
775 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
777 for (ic
= 0; ic
< n_ic
; ic
++) {
778 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
780 /* We shall not do any remixing. Hence, just check by name */
782 m
->map_table_f
[oc
][ic
] = 1.0f
;
787 /* OK, we shall do the full monty: upmixing and downmixing. Our
788 * algorithm is relatively simple, does not do spacialization, delay
789 * elements or apply lowpass filters for LFE. Patches are always
790 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
791 * probably wouldn't make any sense anyway.)
793 * This code is not idempotent: downmixing an upmixed stereo stream is
794 * not identical to the original. The volume will not match, and the
795 * two channels will be a linear combination of both.
797 * This is loosely based on random suggestions found on the Internet,
799 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
802 * The algorithm works basically like this:
804 * 1) Connect all channels with matching names.
807 * S:Mono: Copy into all D:channels
808 * D:Mono: Avg all S:channels
810 * 3) Mix D:Left, D:Right:
811 * D:Left: If not connected, avg all S:Left
812 * D:Right: If not connected, avg all S:Right
815 * If not connected, avg all S:Center
816 * If still not connected, avg all S:Left, S:Right
819 * If not connected, avg all S:*
821 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
822 * connected, mix into all D:left and all D:right channels. Gain is
825 * 7) Make sure S:Center, S:LFE is used:
827 * S:Center, S:LFE: If not connected, mix into all D:left, all
828 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
829 * for LFE. C-front is only mixed into L-front/R-front if available,
830 * otherwise into all L/R channels. Similarly for C-rear.
832 * 8) Normalize each row in the matrix such that the sum for each row is
833 * not larger than 1.0 in order to avoid clipping.
835 * S: and D: shall relate to the source resp. destination channels.
837 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
838 * rear if needed. For 4: we try to find some suitable C source for C,
839 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
840 * channels. For 6: the rear channels should not be dropped entirely,
841 * however have only minimal impact. For 7: movies usually encode
842 * speech on the center channel. Thus we have to make sure this channel
843 * is distributed to L and R if not available in the output. Also, LFE
844 * is used to achieve a greater dynamic range, and thus we should try
845 * to do our best to pass it to L+R.
852 ic_unconnected_left
= 0,
853 ic_unconnected_right
= 0,
854 ic_unconnected_center
= 0,
855 ic_unconnected_lfe
= 0;
856 bool ic_unconnected_center_mixed_in
= 0;
860 for (ic
= 0; ic
< n_ic
; ic
++) {
861 if (on_left(r
->i_cm
.map
[ic
]))
863 if (on_right(r
->i_cm
.map
[ic
]))
865 if (on_center(r
->i_cm
.map
[ic
]))
869 for (oc
= 0; oc
< n_oc
; oc
++) {
870 bool oc_connected
= false;
871 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
873 for (ic
= 0; ic
< n_ic
; ic
++) {
874 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
876 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
877 m
->map_table_f
[oc
][ic
] = 1.0f
;
880 ic_connected
[ic
] = true;
882 else if (b
== PA_CHANNEL_POSITION_MONO
) {
883 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
886 ic_connected
[ic
] = true;
891 /* Try to find matching input ports for this output port */
895 /* We are not connected and on the left side, let's
896 * average all left side input channels. */
899 for (ic
= 0; ic
< n_ic
; ic
++)
900 if (on_left(r
->i_cm
.map
[ic
])) {
901 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
902 ic_connected
[ic
] = true;
905 /* We ignore the case where there is no left input channel.
906 * Something is really wrong in this case anyway. */
908 } else if (on_right(b
)) {
910 /* We are not connected and on the right side, let's
911 * average all right side input channels. */
914 for (ic
= 0; ic
< n_ic
; ic
++)
915 if (on_right(r
->i_cm
.map
[ic
])) {
916 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
917 ic_connected
[ic
] = true;
920 /* We ignore the case where there is no right input
921 * channel. Something is really wrong in this case anyway.
924 } else if (on_center(b
)) {
928 /* We are not connected and at the center. Let's average
929 * all center input channels. */
931 for (ic
= 0; ic
< n_ic
; ic
++)
932 if (on_center(r
->i_cm
.map
[ic
])) {
933 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
934 ic_connected
[ic
] = true;
937 } else if (ic_left
+ ic_right
> 0) {
939 /* Hmm, no center channel around, let's synthesize it
940 * by mixing L and R.*/
942 for (ic
= 0; ic
< n_ic
; ic
++)
943 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
944 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
945 ic_connected
[ic
] = true;
949 /* We ignore the case where there is not even a left or
950 * right input channel. Something is really wrong in this
953 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
955 /* We are not connected and an LFE. Let's average all
956 * channels for LFE. */
958 for (ic
= 0; ic
< n_ic
; ic
++)
959 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
961 /* Please note that a channel connected to LFE doesn't
962 * really count as connected. */
967 for (ic
= 0; ic
< n_ic
; ic
++) {
968 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
970 if (ic_connected
[ic
])
974 ic_unconnected_left
++;
975 else if (on_right(a
))
976 ic_unconnected_right
++;
977 else if (on_center(a
))
978 ic_unconnected_center
++;
980 ic_unconnected_lfe
++;
983 for (ic
= 0; ic
< n_ic
; ic
++) {
984 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
986 if (ic_connected
[ic
])
989 for (oc
= 0; oc
< n_oc
; oc
++) {
990 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
992 if (on_left(a
) && on_left(b
))
993 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
995 else if (on_right(a
) && on_right(b
))
996 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
998 else if (on_center(a
) && on_center(b
)) {
999 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1000 ic_unconnected_center_mixed_in
= true;
1002 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1003 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1007 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1008 unsigned ncenter
[PA_CHANNELS_MAX
];
1009 bool found_frs
[PA_CHANNELS_MAX
];
1011 memset(ncenter
, 0, sizeof(ncenter
));
1012 memset(found_frs
, 0, sizeof(found_frs
));
1014 /* Hmm, as it appears there was no center channel we
1015 could mix our center channel in. In this case, mix it into
1016 left and right. Using .5 as the factor. */
1018 for (ic
= 0; ic
< n_ic
; ic
++) {
1020 if (ic_connected
[ic
])
1023 if (!on_center(r
->i_cm
.map
[ic
]))
1026 for (oc
= 0; oc
< n_oc
; oc
++) {
1028 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1031 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1032 found_frs
[ic
] = true;
1037 for (oc
= 0; oc
< n_oc
; oc
++) {
1039 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1042 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1047 for (oc
= 0; oc
< n_oc
; oc
++) {
1049 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1052 if (ncenter
[oc
] <= 0)
1055 for (ic
= 0; ic
< n_ic
; ic
++) {
1057 if (!on_center(r
->i_cm
.map
[ic
]))
1060 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1061 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1067 for (oc
= 0; oc
< n_oc
; oc
++) {
1069 for (ic
= 0; ic
< n_ic
; ic
++)
1070 sum
+= m
->map_table_f
[oc
][ic
];
1073 for (ic
= 0; ic
< n_ic
; ic
++)
1074 m
->map_table_f
[oc
][ic
] /= sum
;
1077 /* make an 16:16 int version of the matrix */
1078 for (oc
= 0; oc
< n_oc
; oc
++)
1079 for (ic
= 0; ic
< n_ic
; ic
++)
1080 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1082 s
= pa_strbuf_new();
1084 pa_strbuf_printf(s
, " ");
1085 for (ic
= 0; ic
< n_ic
; ic
++)
1086 pa_strbuf_printf(s
, " I%02u ", ic
);
1087 pa_strbuf_puts(s
, "\n +");
1089 for (ic
= 0; ic
< n_ic
; ic
++)
1090 pa_strbuf_printf(s
, "------");
1091 pa_strbuf_puts(s
, "\n");
1093 for (oc
= 0; oc
< n_oc
; oc
++) {
1094 pa_strbuf_printf(s
, "O%02u |", oc
);
1096 for (ic
= 0; ic
< n_ic
; ic
++)
1097 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1099 pa_strbuf_puts(s
, "\n");
1102 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1105 /* initialize the remapping function */
1109 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1115 pa_assert(input
->memblock
);
1117 /* Convert the incoming sample into the work sample format and place them
1118 * in to_work_format_buf. */
1120 if (!r
->to_work_format_func
|| !input
->length
)
1123 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1125 r
->to_work_format_buf
.index
= 0;
1126 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1128 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1129 if (r
->to_work_format_buf
.memblock
)
1130 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1132 r
->to_work_format_buf_samples
= n_samples
;
1133 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1136 src
= pa_memblock_acquire_chunk(input
);
1137 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1139 r
->to_work_format_func(n_samples
, src
, dst
);
1141 pa_memblock_release(input
->memblock
);
1142 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1144 return &r
->to_work_format_buf
;
1147 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1148 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1150 size_t leftover_length
= 0;
1155 pa_assert(input
->memblock
);
1157 /* Remap channels and place the result in remap_buf. There may be leftover
1158 * data in the beginning of remap_buf. The leftover data is already
1159 * remapped, so it's not part of the input, it's part of the output. */
1161 have_leftover
= r
->remap_buf_contains_leftover_data
;
1162 r
->remap_buf_contains_leftover_data
= false;
1164 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1166 else if (input
->length
<= 0)
1167 return &r
->remap_buf
;
1169 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1170 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1172 if (have_leftover
) {
1173 leftover_length
= r
->remap_buf
.length
;
1174 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1177 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1178 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1180 if (have_leftover
) {
1181 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1182 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1184 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1185 dst
= pa_memblock_acquire(new_block
);
1186 memcpy(dst
, src
, leftover_length
);
1187 pa_memblock_release(r
->remap_buf
.memblock
);
1188 pa_memblock_release(new_block
);
1190 pa_memblock_unref(r
->remap_buf
.memblock
);
1191 r
->remap_buf
.memblock
= new_block
;
1192 r
->remap_buf_size
= r
->remap_buf
.length
;
1196 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1197 if (r
->remap_buf
.memblock
)
1198 pa_memblock_unref(r
->remap_buf
.memblock
);
1200 r
->remap_buf_size
= r
->remap_buf
.length
;
1201 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1205 src
= pa_memblock_acquire_chunk(input
);
1206 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1208 if (r
->map_required
) {
1209 pa_remap_t
*remap
= &r
->remap
;
1211 pa_assert(remap
->do_remap
);
1212 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1215 memcpy(dst
, src
, input
->length
);
1217 pa_memblock_release(input
->memblock
);
1218 pa_memblock_release(r
->remap_buf
.memblock
);
1220 return &r
->remap_buf
;
1223 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1224 unsigned in_n_frames
, in_n_samples
;
1225 unsigned out_n_frames
, out_n_samples
;
1230 /* Resample the data and place the result in resample_buf. */
1232 if (!r
->impl_resample
|| !input
->length
)
1235 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1236 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1238 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1239 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1241 r
->resample_buf
.index
= 0;
1242 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1244 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1245 if (r
->resample_buf
.memblock
)
1246 pa_memblock_unref(r
->resample_buf
.memblock
);
1248 r
->resample_buf_samples
= out_n_samples
;
1249 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1252 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1253 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1255 return &r
->resample_buf
;
1258 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1259 unsigned n_samples
, n_frames
;
1265 /* Convert the data into the correct sample type and place the result in
1266 * from_work_format_buf. */
1268 if (!r
->from_work_format_func
|| !input
->length
)
1271 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1272 n_frames
= n_samples
/ r
->o_ss
.channels
;
1274 r
->from_work_format_buf
.index
= 0;
1275 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1277 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1278 if (r
->from_work_format_buf
.memblock
)
1279 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1281 r
->from_work_format_buf_samples
= n_samples
;
1282 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1285 src
= pa_memblock_acquire_chunk(input
);
1286 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1287 r
->from_work_format_func(n_samples
, src
, dst
);
1288 pa_memblock_release(input
->memblock
);
1289 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1291 return &r
->from_work_format_buf
;
1294 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1300 pa_assert(in
->length
);
1301 pa_assert(in
->memblock
);
1302 pa_assert(in
->length
% r
->i_fz
== 0);
1304 buf
= (pa_memchunk
*) in
;
1305 buf
= convert_to_work_format(r
, buf
);
1306 buf
= remap_channels(r
, buf
);
1307 buf
= resample(r
, buf
);
1310 buf
= convert_from_work_format(r
, buf
);
1314 pa_memblock_ref(buf
->memblock
);
1316 pa_memchunk_reset(buf
);
1318 pa_memchunk_reset(out
);
1321 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1328 /* Store the leftover to remap_buf. */
1330 r
->remap_buf
.length
= len
;
1332 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1333 if (r
->remap_buf
.memblock
)
1334 pa_memblock_unref(r
->remap_buf
.memblock
);
1336 r
->remap_buf_size
= r
->remap_buf
.length
;
1337 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1340 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1341 memcpy(dst
, buf
, r
->remap_buf
.length
);
1342 pa_memblock_release(r
->remap_buf
.memblock
);
1344 r
->remap_buf_contains_leftover_data
= true;
1347 /*** libsamplerate based implementation ***/
1349 #ifdef HAVE_LIBSAMPLERATE
1350 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1352 struct src_data
*libsamplerate_data
;
1357 pa_assert(out_n_frames
);
1359 libsamplerate_data
= r
->impl_data
;
1360 memset(&data
, 0, sizeof(data
));
1362 data
.data_in
= pa_memblock_acquire_chunk(input
);
1363 data
.input_frames
= (long int) in_n_frames
;
1365 data
.data_out
= pa_memblock_acquire_chunk(output
);
1366 data
.output_frames
= (long int) *out_n_frames
;
1368 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1369 data
.end_of_input
= 0;
1371 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1373 if (data
.input_frames_used
< in_n_frames
) {
1374 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1375 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1377 save_leftover(r
, leftover_data
, leftover_length
);
1380 pa_memblock_release(input
->memblock
);
1381 pa_memblock_release(output
->memblock
);
1383 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1386 static void libsamplerate_update_rates(pa_resampler
*r
) {
1387 struct src_data
*libsamplerate_data
;
1390 libsamplerate_data
= r
->impl_data
;
1391 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1394 static void libsamplerate_reset(pa_resampler
*r
) {
1395 struct src_data
*libsamplerate_data
;
1398 libsamplerate_data
= r
->impl_data
;
1399 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1402 static void libsamplerate_free(pa_resampler
*r
) {
1403 struct src_data
*libsamplerate_data
;
1406 libsamplerate_data
= r
->impl_data
;
1407 if (libsamplerate_data
->state
)
1408 src_delete(libsamplerate_data
->state
);
1411 static int libsamplerate_init(pa_resampler
*r
) {
1413 struct src_data
*libsamplerate_data
;
1417 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1419 if (!(libsamplerate_data
->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
= libsamplerate_data
;
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 struct speex_data
*speex_data
;
1443 pa_assert(out_n_frames
);
1445 speex_data
= 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(speex_data
->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 struct speex_data
*speex_data
;
1467 pa_assert(out_n_frames
);
1469 speex_data
= 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(speex_data
->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 struct speex_data
*speex_data
;
1487 speex_data
= r
->impl_data
;
1489 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1492 static void speex_reset(pa_resampler
*r
) {
1493 struct speex_data
*speex_data
;
1496 speex_data
= r
->impl_data
;
1498 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1501 static void speex_free(pa_resampler
*r
) {
1502 struct speex_data
*speex_data
;
1505 speex_data
= r
->impl_data
;
1506 if (!speex_data
->state
)
1509 speex_resampler_destroy(speex_data
->state
);
1512 static int speex_init(pa_resampler
*r
) {
1514 struct speex_data
*speex_data
;
1518 speex_data
= pa_xnew(struct speex_data
, 1);
1520 r
->impl_free
= speex_free
;
1521 r
->impl_update_rates
= speex_update_rates
;
1522 r
->impl_reset
= speex_reset
;
1523 r
->impl_data
= speex_data
;
1525 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1527 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1528 r
->impl_resample
= speex_resample_int
;
1531 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1533 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1534 r
->impl_resample
= speex_resample_float
;
1537 pa_log_info("Choosing speex quality setting %i.", q
);
1539 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1546 /* Trivial implementation */
1548 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1550 unsigned i_index
, o_index
;
1552 struct trivial_data
*trivial_data
;
1557 pa_assert(out_n_frames
);
1559 trivial_data
= r
->impl_data
;
1560 fz
= r
->w_sz
* r
->o_ss
.channels
;
1562 src
= pa_memblock_acquire_chunk(input
);
1563 dst
= pa_memblock_acquire_chunk(output
);
1565 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1566 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1567 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1569 if (i_index
>= in_n_frames
)
1572 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1574 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1577 pa_memblock_release(input
->memblock
);
1578 pa_memblock_release(output
->memblock
);
1580 *out_n_frames
= o_index
;
1582 trivial_data
->i_counter
+= in_n_frames
;
1584 /* Normalize counters */
1585 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1586 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1588 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1589 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1593 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1594 struct trivial_data
*trivial_data
;
1597 trivial_data
= r
->impl_data
;
1599 trivial_data
->i_counter
= 0;
1600 trivial_data
->o_counter
= 0;
1603 static int trivial_init(pa_resampler
*r
) {
1604 struct trivial_data
*trivial_data
;
1607 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1609 r
->impl_resample
= trivial_resample
;
1610 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1611 r
->impl_reset
= trivial_update_rates_or_reset
;
1612 r
->impl_data
= trivial_data
;
1617 /* Peak finder implementation */
1619 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1620 unsigned c
, o_index
= 0;
1621 unsigned i
, i_end
= 0;
1623 struct peaks_data
*peaks_data
;
1628 pa_assert(out_n_frames
);
1630 peaks_data
= r
->impl_data
;
1631 src
= pa_memblock_acquire_chunk(input
);
1632 dst
= pa_memblock_acquire_chunk(output
);
1634 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1635 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1637 while (i_end
< in_n_frames
) {
1638 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1639 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1641 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1643 /* 1ch float is treated separately, because that is the common case */
1644 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1645 float *s
= (float*) src
+ i
;
1646 float *d
= (float*) dst
+ o_index
;
1648 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1649 float n
= fabsf(*s
++);
1651 if (n
> peaks_data
->max_f
[0])
1652 peaks_data
->max_f
[0] = n
;
1656 *d
= peaks_data
->max_f
[0];
1657 peaks_data
->max_f
[0] = 0;
1658 o_index
++, peaks_data
->o_counter
++;
1660 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1661 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1662 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1664 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1665 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1666 int16_t n
= abs(*s
++);
1668 if (n
> peaks_data
->max_i
[c
])
1669 peaks_data
->max_i
[c
] = n
;
1673 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1674 *d
= peaks_data
->max_i
[c
];
1675 peaks_data
->max_i
[c
] = 0;
1677 o_index
++, peaks_data
->o_counter
++;
1680 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1681 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1683 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1684 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1685 float n
= fabsf(*s
++);
1687 if (n
> peaks_data
->max_f
[c
])
1688 peaks_data
->max_f
[c
] = n
;
1692 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1693 *d
= peaks_data
->max_f
[c
];
1694 peaks_data
->max_f
[c
] = 0;
1696 o_index
++, peaks_data
->o_counter
++;
1701 pa_memblock_release(input
->memblock
);
1702 pa_memblock_release(output
->memblock
);
1704 *out_n_frames
= o_index
;
1706 peaks_data
->i_counter
+= in_n_frames
;
1708 /* Normalize counters */
1709 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1710 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1712 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1713 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1717 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1718 struct peaks_data
*peaks_data
;
1721 peaks_data
= r
->impl_data
;
1723 peaks_data
->i_counter
= 0;
1724 peaks_data
->o_counter
= 0;
1727 static int peaks_init(pa_resampler
*r
) {
1728 struct peaks_data
*peaks_data
;
1730 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1731 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1733 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1735 r
->impl_resample
= peaks_resample
;
1736 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1737 r
->impl_reset
= peaks_update_rates_or_reset
;
1738 r
->impl_data
= peaks_data
;
1743 /*** ffmpeg based implementation ***/
1745 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1746 unsigned used_frames
= 0, c
;
1747 int previous_consumed_frames
= -1;
1748 struct ffmpeg_data
*ffmpeg_data
;
1753 pa_assert(out_n_frames
);
1755 ffmpeg_data
= r
->impl_data
;
1757 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1760 int16_t *p
, *t
, *k
, *q
, *s
;
1761 int consumed_frames
;
1763 /* Allocate a new block */
1764 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1765 p
= pa_memblock_acquire(b
);
1767 /* Now copy the input data, splitting up channels */
1768 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1770 for (u
= 0; u
< in_n_frames
; u
++) {
1772 t
+= r
->o_ss
.channels
;
1775 pa_memblock_release(input
->memblock
);
1777 /* Allocate buffer for the result */
1778 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1779 q
= pa_memblock_acquire(w
);
1782 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1785 (int) in_n_frames
, (int) *out_n_frames
,
1786 c
>= (unsigned) (r
->o_ss
.channels
-1));
1788 pa_memblock_release(b
);
1789 pa_memblock_unref(b
);
1791 pa_assert(consumed_frames
<= (int) in_n_frames
);
1792 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1793 previous_consumed_frames
= consumed_frames
;
1795 /* And place the results in the output buffer */
1796 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1797 for (u
= 0; u
< used_frames
; u
++) {
1800 s
+= r
->o_ss
.channels
;
1802 pa_memblock_release(output
->memblock
);
1803 pa_memblock_release(w
);
1804 pa_memblock_unref(w
);
1807 if (previous_consumed_frames
< (int) in_n_frames
) {
1808 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1809 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1811 save_leftover(r
, leftover_data
, leftover_length
);
1812 pa_memblock_release(input
->memblock
);
1815 *out_n_frames
= used_frames
;
1818 static void ffmpeg_free(pa_resampler
*r
) {
1820 struct ffmpeg_data
*ffmpeg_data
;
1824 ffmpeg_data
= r
->impl_data
;
1825 if (ffmpeg_data
->state
)
1826 av_resample_close(ffmpeg_data
->state
);
1828 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1829 if (ffmpeg_data
->buf
[c
].memblock
)
1830 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1833 static int ffmpeg_init(pa_resampler
*r
) {
1835 struct ffmpeg_data
*ffmpeg_data
;
1839 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1841 /* We could probably implement different quality levels by
1842 * adjusting the filter parameters here. However, ffmpeg
1843 * internally only uses these hardcoded values, so let's use them
1844 * here for now as well until ffmpeg makes this configurable. */
1846 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1849 r
->impl_free
= ffmpeg_free
;
1850 r
->impl_resample
= ffmpeg_resample
;
1851 r
->impl_data
= (void *) ffmpeg_data
;
1853 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1854 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1859 /*** copy (noop) implementation ***/
1861 static int copy_init(pa_resampler
*r
) {
1864 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);