2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
33 #include <speex/speex_resampler.h>
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/remap.h>
42 #include <pulsecore/core-util.h>
43 #include "ffmpeg/avcodec.h"
45 #include "resampler.h"
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
51 pa_resample_method_t method
;
52 pa_resample_flags_t flags
;
54 pa_sample_spec i_ss
, o_ss
;
55 pa_channel_map i_cm
, o_cm
;
56 size_t i_fz
, o_fz
, w_fz
, w_sz
;
59 pa_memchunk to_work_format_buf
;
60 pa_memchunk remap_buf
;
61 pa_memchunk resample_buf
;
62 pa_memchunk from_work_format_buf
;
63 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
;
70 uint8_t work_channels
;
72 pa_convert_func_t to_work_format_func
;
73 pa_convert_func_t from_work_format_func
;
78 pa_resampler_impl impl
;
81 struct trivial_data
{ /* data specific to the trivial resampler */
86 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
90 float max_f
[PA_CHANNELS_MAX
];
91 int16_t max_i
[PA_CHANNELS_MAX
];
94 struct ffmpeg_data
{ /* data specific to ffmpeg */
95 struct AVResampleContext
*state
;
96 pa_memchunk buf
[PA_CHANNELS_MAX
];
99 static int copy_init(pa_resampler
*r
);
100 static int trivial_init(pa_resampler
*r
);
102 static int speex_init(pa_resampler
*r
);
104 static int ffmpeg_init(pa_resampler
*r
);
105 static int peaks_init(pa_resampler
*r
);
106 #ifdef HAVE_LIBSAMPLERATE
107 static int libsamplerate_init(pa_resampler
*r
);
110 static void calc_map_table(pa_resampler
*r
);
112 static int (* const init_table
[])(pa_resampler
*r
) = {
113 #ifdef HAVE_LIBSAMPLERATE
114 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
115 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
116 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
117 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
118 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
120 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
121 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
122 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
123 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
124 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
126 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
128 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
129 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
130 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
131 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
132 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
133 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
134 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
135 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
136 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
137 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
138 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
139 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
140 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
141 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
142 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
143 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
154 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
155 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
156 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
157 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
158 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
159 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
160 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
161 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
165 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
166 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
167 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
168 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
169 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
170 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
171 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
172 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
174 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
175 [PA_RESAMPLER_AUTO
] = NULL
,
176 [PA_RESAMPLER_COPY
] = copy_init
,
177 [PA_RESAMPLER_PEAKS
] = peaks_init
,
180 static pa_resample_method_t
choose_auto_resampler(pa_resample_flags_t flags
) {
181 pa_resample_method_t method
;
183 if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1))
184 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
185 else if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
186 method
= PA_RESAMPLER_TRIVIAL
;
188 method
= PA_RESAMPLER_FFMPEG
;
193 static pa_resample_method_t
pa_resampler_fix_method(
194 pa_resample_flags_t flags
,
195 pa_resample_method_t method
,
196 const uint32_t rate_a
,
197 const uint32_t rate_b
) {
199 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
200 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
201 pa_assert(method
>= 0);
202 pa_assert(method
< PA_RESAMPLER_MAX
);
204 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
205 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
206 method
= PA_RESAMPLER_COPY
;
209 if (!pa_resample_method_supported(method
)) {
210 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
211 method
= PA_RESAMPLER_AUTO
;
215 case PA_RESAMPLER_COPY
:
216 if (rate_a
!= rate_b
) {
217 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
218 method
= PA_RESAMPLER_AUTO
;
221 /* Else fall through */
222 case PA_RESAMPLER_FFMPEG
:
223 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
224 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
225 method
= PA_RESAMPLER_AUTO
;
229 /* The Peaks resampler only supports downsampling.
230 * Revert to auto if we are upsampling */
231 case PA_RESAMPLER_PEAKS
:
232 if (rate_a
< rate_b
) {
233 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
234 method
= PA_RESAMPLER_AUTO
;
242 if (method
== PA_RESAMPLER_AUTO
)
243 method
= choose_auto_resampler(flags
);
248 /* Return true if a is a more precise sample format than b, else return false */
249 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
250 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
251 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
260 case PA_SAMPLE_S16LE
:
261 case PA_SAMPLE_S16BE
:
262 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
268 case PA_SAMPLE_S24LE
:
269 case PA_SAMPLE_S24BE
:
270 case PA_SAMPLE_S24_32LE
:
271 case PA_SAMPLE_S24_32BE
:
272 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
273 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
279 case PA_SAMPLE_FLOAT32LE
:
280 case PA_SAMPLE_FLOAT32BE
:
281 case PA_SAMPLE_S32LE
:
282 case PA_SAMPLE_S32BE
:
283 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
284 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
295 static pa_sample_format_t
pa_resampler_choose_work_format(
296 pa_resample_method_t method
,
297 pa_sample_format_t a
,
298 pa_sample_format_t b
,
300 pa_sample_format_t work_format
;
302 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
303 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
304 pa_assert(method
>= 0);
305 pa_assert(method
< PA_RESAMPLER_MAX
);
307 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
308 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
311 /* This block is for resampling functions that only
312 * support the S16 sample format. */
313 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
314 case PA_RESAMPLER_FFMPEG
:
315 work_format
= PA_SAMPLE_S16NE
;
318 /* This block is for resampling functions that support
319 * any sample format. */
320 case PA_RESAMPLER_COPY
: /* fall through */
321 case PA_RESAMPLER_TRIVIAL
:
322 if (!map_required
&& a
== b
) {
326 /* Else fall trough */
327 case PA_RESAMPLER_PEAKS
:
328 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
329 work_format
= PA_SAMPLE_S16NE
;
330 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
331 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
332 work_format
= PA_SAMPLE_FLOAT32NE
;
334 work_format
= PA_SAMPLE_S16NE
;
338 work_format
= PA_SAMPLE_FLOAT32NE
;
344 pa_resampler
* pa_resampler_new(
346 const pa_sample_spec
*a
,
347 const pa_channel_map
*am
,
348 const pa_sample_spec
*b
,
349 const pa_channel_map
*bm
,
350 pa_resample_method_t method
,
351 pa_resample_flags_t flags
) {
353 pa_resampler
*r
= NULL
;
358 pa_assert(pa_sample_spec_valid(a
));
359 pa_assert(pa_sample_spec_valid(b
));
360 pa_assert(method
>= 0);
361 pa_assert(method
< PA_RESAMPLER_MAX
);
363 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
365 r
= pa_xnew0(pa_resampler
, 1);
370 /* Fill sample specs */
374 /* set up the remap structure */
375 r
->remap
.i_ss
= &r
->i_ss
;
376 r
->remap
.o_ss
= &r
->o_ss
;
377 r
->remap
.format
= &r
->work_format
;
381 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
386 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
389 r
->i_fz
= pa_frame_size(a
);
390 r
->o_fz
= pa_frame_size(b
);
394 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
396 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
398 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
400 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
402 if (r
->i_ss
.format
!= r
->work_format
) {
403 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
404 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
407 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
408 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
413 if (r
->o_ss
.format
!= r
->work_format
) {
414 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
415 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
418 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
419 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
424 r
->work_channels
= r
->o_ss
.channels
;
425 r
->w_fz
= pa_sample_size_of_format(r
->work_format
) * r
->work_channels
;
427 /* initialize implementation */
428 if (init_table
[method
](r
) < 0)
439 void pa_resampler_free(pa_resampler
*r
) {
445 pa_xfree(r
->impl
.data
);
447 if (r
->to_work_format_buf
.memblock
)
448 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
449 if (r
->remap_buf
.memblock
)
450 pa_memblock_unref(r
->remap_buf
.memblock
);
451 if (r
->resample_buf
.memblock
)
452 pa_memblock_unref(r
->resample_buf
.memblock
);
453 if (r
->from_work_format_buf
.memblock
)
454 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
459 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
462 pa_assert(r
->impl
.update_rates
);
464 if (r
->i_ss
.rate
== rate
)
469 r
->impl
.update_rates(r
);
472 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
475 pa_assert(r
->impl
.update_rates
);
477 if (r
->o_ss
.rate
== rate
)
482 r
->impl
.update_rates(r
);
485 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
488 /* Let's round up here to make it more likely that the caller will get at
489 * least out_length amount of data from pa_resampler_run().
491 * We don't take the leftover into account here. If we did, then it might
492 * be in theory possible that this function would return 0 and
493 * pa_resampler_run() would also return 0. That could lead to infinite
494 * loops. When the leftover is ignored here, such loops would eventually
495 * terminate, because the leftover would grow each round, finally
496 * surpassing the minimum input threshold of the resampler. */
497 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
;
500 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
505 /* Let's round up here to ensure that the caller will always allocate big
506 * enough output buffer. */
508 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
510 if (r
->remap_buf_contains_leftover_data
)
511 frames
+= r
->remap_buf
.length
/ r
->w_fz
;
513 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
516 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
517 size_t block_size_max
;
518 pa_sample_spec max_ss
;
524 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
526 /* We deduce the "largest" sample spec we're using during the
528 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
530 /* We silently assume that the format enum is ordered by size */
531 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
532 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
534 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
536 max_fs
= pa_frame_size(&max_ss
);
537 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
539 if (r
->remap_buf_contains_leftover_data
)
540 frames
-= r
->remap_buf
.length
/ r
->w_fz
;
542 block_size_max
= ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
544 if (block_size_max
> 0)
545 return block_size_max
;
547 /* A single input frame may result in so much output that it doesn't
548 * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
549 * this case the max block size will be set to one frame, and some
550 * memory will be probably be allocated with malloc() instead of using
553 * XXX: Should we support this case at all? We could also refuse to
554 * create resamplers whose max block size would exceed the memory pool
555 * block size. In this case also updating the resampler rate should
556 * fail if the new rate would cause an excessive max block size (in
557 * which case the stream would probably have to be killed). */
561 void pa_resampler_reset(pa_resampler
*r
) {
567 r
->remap_buf_contains_leftover_data
= false;
570 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
576 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
582 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
588 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
594 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
600 static const char * const resample_methods
[] = {
601 "src-sinc-best-quality",
602 "src-sinc-medium-quality",
604 "src-zero-order-hold",
635 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
637 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
640 return resample_methods
[m
];
643 int pa_resample_method_supported(pa_resample_method_t m
) {
645 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
648 #ifndef HAVE_LIBSAMPLERATE
649 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
654 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
656 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
663 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
664 pa_resample_method_t m
;
668 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
669 if (pa_streq(string
, resample_methods
[m
]))
672 if (pa_streq(string
, "speex-fixed"))
673 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
675 if (pa_streq(string
, "speex-float"))
676 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
678 return PA_RESAMPLER_INVALID
;
681 static bool on_left(pa_channel_position_t p
) {
684 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
685 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
686 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
687 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
688 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
689 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
692 static bool on_right(pa_channel_position_t p
) {
695 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
696 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
697 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
698 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
699 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
700 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
703 static bool on_center(pa_channel_position_t p
) {
706 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
707 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
708 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
709 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
710 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
713 static bool on_lfe(pa_channel_position_t p
) {
715 p
== PA_CHANNEL_POSITION_LFE
;
718 static bool on_front(pa_channel_position_t p
) {
720 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
721 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
722 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
723 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
724 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
725 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
726 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
727 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
730 static bool on_rear(pa_channel_position_t p
) {
732 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
733 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
734 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
735 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
736 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
737 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
740 static bool on_side(pa_channel_position_t p
) {
742 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
743 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
744 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
754 static int front_rear_side(pa_channel_position_t p
) {
764 static void calc_map_table(pa_resampler
*r
) {
767 bool ic_connected
[PA_CHANNELS_MAX
];
775 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
)))))
780 n_oc
= r
->o_ss
.channels
;
781 n_ic
= r
->i_ss
.channels
;
783 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
784 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
786 memset(ic_connected
, 0, sizeof(ic_connected
));
787 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
789 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
792 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
793 m
->map_table_f
[oc
][oc
] = 1.0f
;
795 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
797 for (oc
= 0; oc
< n_oc
; oc
++) {
798 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
800 for (ic
= 0; ic
< n_ic
; ic
++) {
801 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
803 /* We shall not do any remixing. Hence, just check by name */
805 m
->map_table_f
[oc
][ic
] = 1.0f
;
810 /* OK, we shall do the full monty: upmixing and downmixing. Our
811 * algorithm is relatively simple, does not do spacialization, delay
812 * elements or apply lowpass filters for LFE. Patches are always
813 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
814 * probably wouldn't make any sense anyway.)
816 * This code is not idempotent: downmixing an upmixed stereo stream is
817 * not identical to the original. The volume will not match, and the
818 * two channels will be a linear combination of both.
820 * This is loosely based on random suggestions found on the Internet,
822 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
825 * The algorithm works basically like this:
827 * 1) Connect all channels with matching names.
830 * S:Mono: Copy into all D:channels
831 * D:Mono: Avg all S:channels
833 * 3) Mix D:Left, D:Right:
834 * D:Left: If not connected, avg all S:Left
835 * D:Right: If not connected, avg all S:Right
838 * If not connected, avg all S:Center
839 * If still not connected, avg all S:Left, S:Right
842 * If not connected, avg all S:*
844 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
845 * connected, mix into all D:left and all D:right channels. Gain is
848 * 7) Make sure S:Center, S:LFE is used:
850 * S:Center, S:LFE: If not connected, mix into all D:left, all
851 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
852 * for LFE. C-front is only mixed into L-front/R-front if available,
853 * otherwise into all L/R channels. Similarly for C-rear.
855 * 8) Normalize each row in the matrix such that the sum for each row is
856 * not larger than 1.0 in order to avoid clipping.
858 * S: and D: shall relate to the source resp. destination channels.
860 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
861 * rear if needed. For 4: we try to find some suitable C source for C,
862 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
863 * channels. For 6: the rear channels should not be dropped entirely,
864 * however have only minimal impact. For 7: movies usually encode
865 * speech on the center channel. Thus we have to make sure this channel
866 * is distributed to L and R if not available in the output. Also, LFE
867 * is used to achieve a greater dynamic range, and thus we should try
868 * to do our best to pass it to L+R.
875 ic_unconnected_left
= 0,
876 ic_unconnected_right
= 0,
877 ic_unconnected_center
= 0,
878 ic_unconnected_lfe
= 0;
879 bool ic_unconnected_center_mixed_in
= 0;
883 for (ic
= 0; ic
< n_ic
; ic
++) {
884 if (on_left(r
->i_cm
.map
[ic
]))
886 if (on_right(r
->i_cm
.map
[ic
]))
888 if (on_center(r
->i_cm
.map
[ic
]))
892 for (oc
= 0; oc
< n_oc
; oc
++) {
893 bool oc_connected
= false;
894 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
896 for (ic
= 0; ic
< n_ic
; ic
++) {
897 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
899 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
900 m
->map_table_f
[oc
][ic
] = 1.0f
;
903 ic_connected
[ic
] = true;
905 else if (b
== PA_CHANNEL_POSITION_MONO
) {
906 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
909 ic_connected
[ic
] = true;
914 /* Try to find matching input ports for this output port */
918 /* We are not connected and on the left side, let's
919 * average all left side input channels. */
922 for (ic
= 0; ic
< n_ic
; ic
++)
923 if (on_left(r
->i_cm
.map
[ic
])) {
924 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
925 ic_connected
[ic
] = true;
928 /* We ignore the case where there is no left input channel.
929 * Something is really wrong in this case anyway. */
931 } else if (on_right(b
)) {
933 /* We are not connected and on the right side, let's
934 * average all right side input channels. */
937 for (ic
= 0; ic
< n_ic
; ic
++)
938 if (on_right(r
->i_cm
.map
[ic
])) {
939 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
940 ic_connected
[ic
] = true;
943 /* We ignore the case where there is no right input
944 * channel. Something is really wrong in this case anyway.
947 } else if (on_center(b
)) {
951 /* We are not connected and at the center. Let's average
952 * all center input channels. */
954 for (ic
= 0; ic
< n_ic
; ic
++)
955 if (on_center(r
->i_cm
.map
[ic
])) {
956 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
957 ic_connected
[ic
] = true;
960 } else if (ic_left
+ ic_right
> 0) {
962 /* Hmm, no center channel around, let's synthesize it
963 * by mixing L and R.*/
965 for (ic
= 0; ic
< n_ic
; ic
++)
966 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
967 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
968 ic_connected
[ic
] = true;
972 /* We ignore the case where there is not even a left or
973 * right input channel. Something is really wrong in this
976 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
978 /* We are not connected and an LFE. Let's average all
979 * channels for LFE. */
981 for (ic
= 0; ic
< n_ic
; ic
++)
982 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
984 /* Please note that a channel connected to LFE doesn't
985 * really count as connected. */
990 for (ic
= 0; ic
< n_ic
; ic
++) {
991 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
993 if (ic_connected
[ic
])
997 ic_unconnected_left
++;
998 else if (on_right(a
))
999 ic_unconnected_right
++;
1000 else if (on_center(a
))
1001 ic_unconnected_center
++;
1003 ic_unconnected_lfe
++;
1006 for (ic
= 0; ic
< n_ic
; ic
++) {
1007 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
1009 if (ic_connected
[ic
])
1012 for (oc
= 0; oc
< n_oc
; oc
++) {
1013 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
1015 if (on_left(a
) && on_left(b
))
1016 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1018 else if (on_right(a
) && on_right(b
))
1019 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1021 else if (on_center(a
) && on_center(b
)) {
1022 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1023 ic_unconnected_center_mixed_in
= true;
1025 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1026 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1030 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1031 unsigned ncenter
[PA_CHANNELS_MAX
];
1032 bool found_frs
[PA_CHANNELS_MAX
];
1034 memset(ncenter
, 0, sizeof(ncenter
));
1035 memset(found_frs
, 0, sizeof(found_frs
));
1037 /* Hmm, as it appears there was no center channel we
1038 could mix our center channel in. In this case, mix it into
1039 left and right. Using .5 as the factor. */
1041 for (ic
= 0; ic
< n_ic
; ic
++) {
1043 if (ic_connected
[ic
])
1046 if (!on_center(r
->i_cm
.map
[ic
]))
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 (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1055 found_frs
[ic
] = true;
1060 for (oc
= 0; oc
< n_oc
; oc
++) {
1062 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1065 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1070 for (oc
= 0; oc
< n_oc
; oc
++) {
1072 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1075 if (ncenter
[oc
] <= 0)
1078 for (ic
= 0; ic
< n_ic
; ic
++) {
1080 if (!on_center(r
->i_cm
.map
[ic
]))
1083 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1084 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1090 for (oc
= 0; oc
< n_oc
; oc
++) {
1092 for (ic
= 0; ic
< n_ic
; ic
++)
1093 sum
+= m
->map_table_f
[oc
][ic
];
1096 for (ic
= 0; ic
< n_ic
; ic
++)
1097 m
->map_table_f
[oc
][ic
] /= sum
;
1100 /* make an 16:16 int version of the matrix */
1101 for (oc
= 0; oc
< n_oc
; oc
++)
1102 for (ic
= 0; ic
< n_ic
; ic
++)
1103 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1105 s
= pa_strbuf_new();
1107 pa_strbuf_printf(s
, " ");
1108 for (ic
= 0; ic
< n_ic
; ic
++)
1109 pa_strbuf_printf(s
, " I%02u ", ic
);
1110 pa_strbuf_puts(s
, "\n +");
1112 for (ic
= 0; ic
< n_ic
; ic
++)
1113 pa_strbuf_printf(s
, "------");
1114 pa_strbuf_puts(s
, "\n");
1116 for (oc
= 0; oc
< n_oc
; oc
++) {
1117 pa_strbuf_printf(s
, "O%02u |", oc
);
1119 for (ic
= 0; ic
< n_ic
; ic
++)
1120 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1122 pa_strbuf_puts(s
, "\n");
1125 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1128 /* initialize the remapping function */
1132 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1138 pa_assert(input
->memblock
);
1140 /* Convert the incoming sample into the work sample format and place them
1141 * in to_work_format_buf. */
1143 if (!r
->to_work_format_func
|| !input
->length
)
1146 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1148 r
->to_work_format_buf
.index
= 0;
1149 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1151 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1152 if (r
->to_work_format_buf
.memblock
)
1153 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1155 r
->to_work_format_buf_samples
= n_samples
;
1156 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1159 src
= pa_memblock_acquire_chunk(input
);
1160 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1162 r
->to_work_format_func(n_samples
, src
, dst
);
1164 pa_memblock_release(input
->memblock
);
1165 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1167 return &r
->to_work_format_buf
;
1170 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1171 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1173 size_t leftover_length
= 0;
1178 pa_assert(input
->memblock
);
1180 /* Remap channels and place the result in remap_buf. There may be leftover
1181 * data in the beginning of remap_buf. The leftover data is already
1182 * remapped, so it's not part of the input, it's part of the output. */
1184 have_leftover
= r
->remap_buf_contains_leftover_data
;
1185 r
->remap_buf_contains_leftover_data
= false;
1187 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1189 else if (input
->length
<= 0)
1190 return &r
->remap_buf
;
1192 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1193 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1195 if (have_leftover
) {
1196 leftover_length
= r
->remap_buf
.length
;
1197 out_n_frames
+= leftover_length
/ r
->w_fz
;
1200 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1201 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1203 if (have_leftover
) {
1204 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1205 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1207 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1208 dst
= pa_memblock_acquire(new_block
);
1209 memcpy(dst
, src
, leftover_length
);
1210 pa_memblock_release(r
->remap_buf
.memblock
);
1211 pa_memblock_release(new_block
);
1213 pa_memblock_unref(r
->remap_buf
.memblock
);
1214 r
->remap_buf
.memblock
= new_block
;
1215 r
->remap_buf_size
= r
->remap_buf
.length
;
1219 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1220 if (r
->remap_buf
.memblock
)
1221 pa_memblock_unref(r
->remap_buf
.memblock
);
1223 r
->remap_buf_size
= r
->remap_buf
.length
;
1224 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1228 src
= pa_memblock_acquire_chunk(input
);
1229 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1231 if (r
->map_required
) {
1232 pa_remap_t
*remap
= &r
->remap
;
1234 pa_assert(remap
->do_remap
);
1235 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1238 memcpy(dst
, src
, input
->length
);
1240 pa_memblock_release(input
->memblock
);
1241 pa_memblock_release(r
->remap_buf
.memblock
);
1243 return &r
->remap_buf
;
1246 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1253 /* Store the leftover to remap_buf. */
1255 r
->remap_buf
.length
= len
;
1257 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1258 if (r
->remap_buf
.memblock
)
1259 pa_memblock_unref(r
->remap_buf
.memblock
);
1261 r
->remap_buf_size
= r
->remap_buf
.length
;
1262 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1265 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1266 memcpy(dst
, buf
, r
->remap_buf
.length
);
1267 pa_memblock_release(r
->remap_buf
.memblock
);
1269 r
->remap_buf_contains_leftover_data
= true;
1272 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1273 unsigned in_n_frames
, in_n_samples
;
1274 unsigned out_n_frames
, out_n_samples
;
1275 unsigned leftover_n_frames
;
1280 /* Resample the data and place the result in resample_buf. */
1282 if (!r
->impl
.resample
|| !input
->length
)
1285 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1286 in_n_frames
= (unsigned) (in_n_samples
/ r
->work_channels
);
1288 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1289 out_n_samples
= out_n_frames
* r
->work_channels
;
1291 r
->resample_buf
.index
= 0;
1292 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1294 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1295 if (r
->resample_buf
.memblock
)
1296 pa_memblock_unref(r
->resample_buf
.memblock
);
1298 r
->resample_buf_samples
= out_n_samples
;
1299 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1302 leftover_n_frames
= r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1304 if (leftover_n_frames
> 0) {
1305 void *leftover_data
= (uint8_t *) pa_memblock_acquire_chunk(input
) + (in_n_frames
- leftover_n_frames
) * r
->w_fz
;
1306 save_leftover(r
, leftover_data
, leftover_n_frames
* r
->w_fz
);
1307 pa_memblock_release(input
->memblock
);
1310 r
->resample_buf
.length
= out_n_frames
* r
->w_fz
;
1312 return &r
->resample_buf
;
1315 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1316 unsigned n_samples
, n_frames
;
1322 /* Convert the data into the correct sample type and place the result in
1323 * from_work_format_buf. */
1325 if (!r
->from_work_format_func
|| !input
->length
)
1328 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1329 n_frames
= n_samples
/ r
->o_ss
.channels
;
1331 r
->from_work_format_buf
.index
= 0;
1332 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1334 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1335 if (r
->from_work_format_buf
.memblock
)
1336 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1338 r
->from_work_format_buf_samples
= n_samples
;
1339 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1342 src
= pa_memblock_acquire_chunk(input
);
1343 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1344 r
->from_work_format_func(n_samples
, src
, dst
);
1345 pa_memblock_release(input
->memblock
);
1346 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1348 return &r
->from_work_format_buf
;
1351 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1357 pa_assert(in
->length
);
1358 pa_assert(in
->memblock
);
1359 pa_assert(in
->length
% r
->i_fz
== 0);
1361 buf
= (pa_memchunk
*) in
;
1362 buf
= convert_to_work_format(r
, buf
);
1363 buf
= remap_channels(r
, buf
);
1364 buf
= resample(r
, buf
);
1367 buf
= convert_from_work_format(r
, buf
);
1371 pa_memblock_ref(buf
->memblock
);
1373 pa_memchunk_reset(buf
);
1375 pa_memchunk_reset(out
);
1378 /*** libsamplerate based implementation ***/
1380 #ifdef HAVE_LIBSAMPLERATE
1381 static unsigned libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1388 pa_assert(out_n_frames
);
1390 state
= r
->impl
.data
;
1391 memset(&data
, 0, sizeof(data
));
1393 data
.data_in
= pa_memblock_acquire_chunk(input
);
1394 data
.input_frames
= (long int) in_n_frames
;
1396 data
.data_out
= pa_memblock_acquire_chunk(output
);
1397 data
.output_frames
= (long int) *out_n_frames
;
1399 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1400 data
.end_of_input
= 0;
1402 pa_assert_se(src_process(state
, &data
) == 0);
1404 pa_memblock_release(input
->memblock
);
1405 pa_memblock_release(output
->memblock
);
1407 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1409 return in_n_frames
- data
.input_frames_used
;
1412 static void libsamplerate_update_rates(pa_resampler
*r
) {
1416 state
= r
->impl
.data
;
1417 pa_assert_se(src_set_ratio(state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1420 static void libsamplerate_reset(pa_resampler
*r
) {
1424 state
= r
->impl
.data
;
1425 pa_assert_se(src_reset(state
) == 0);
1428 static void libsamplerate_free(pa_resampler
*r
) {
1432 state
= r
->impl
.data
;
1437 static int libsamplerate_init(pa_resampler
*r
) {
1443 if (!(state
= src_new(r
->method
, r
->work_channels
, &err
)))
1446 r
->impl
.free
= libsamplerate_free
;
1447 r
->impl
.update_rates
= libsamplerate_update_rates
;
1448 r
->impl
.resample
= libsamplerate_resample
;
1449 r
->impl
.reset
= libsamplerate_reset
;
1450 r
->impl
.data
= state
;
1457 /*** speex based implementation ***/
1459 static unsigned speex_resample_float(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_float(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
;
1485 static unsigned speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1487 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1488 SpeexResamplerState
*state
;
1493 pa_assert(out_n_frames
);
1495 state
= r
->impl
.data
;
1497 in
= pa_memblock_acquire_chunk(input
);
1498 out
= pa_memblock_acquire_chunk(output
);
1500 pa_assert_se(speex_resampler_process_interleaved_int(state
, in
, &inf
, out
, &outf
) == 0);
1502 pa_memblock_release(input
->memblock
);
1503 pa_memblock_release(output
->memblock
);
1505 pa_assert(inf
== in_n_frames
);
1506 *out_n_frames
= outf
;
1511 static void speex_update_rates(pa_resampler
*r
) {
1512 SpeexResamplerState
*state
;
1515 state
= r
->impl
.data
;
1517 pa_assert_se(speex_resampler_set_rate(state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1520 static void speex_reset(pa_resampler
*r
) {
1521 SpeexResamplerState
*state
;
1524 state
= r
->impl
.data
;
1526 pa_assert_se(speex_resampler_reset_mem(state
) == 0);
1529 static void speex_free(pa_resampler
*r
) {
1530 SpeexResamplerState
*state
;
1533 state
= r
->impl
.data
;
1537 speex_resampler_destroy(state
);
1540 static int speex_init(pa_resampler
*r
) {
1542 SpeexResamplerState
*state
;
1546 r
->impl
.free
= speex_free
;
1547 r
->impl
.update_rates
= speex_update_rates
;
1548 r
->impl
.reset
= speex_reset
;
1550 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1552 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1553 r
->impl
.resample
= speex_resample_int
;
1556 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1558 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1559 r
->impl
.resample
= speex_resample_float
;
1562 pa_log_info("Choosing speex quality setting %i.", q
);
1564 if (!(state
= speex_resampler_init(r
->work_channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1567 r
->impl
.data
= state
;
1573 /* Trivial implementation */
1575 static unsigned trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1576 unsigned i_index
, o_index
;
1578 struct trivial_data
*trivial_data
;
1583 pa_assert(out_n_frames
);
1585 trivial_data
= r
->impl
.data
;
1587 src
= pa_memblock_acquire_chunk(input
);
1588 dst
= pa_memblock_acquire_chunk(output
);
1590 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1591 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1592 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1594 if (i_index
>= in_n_frames
)
1597 pa_assert_fp(o_index
* r
->w_fz
< pa_memblock_get_length(output
->memblock
));
1599 memcpy((uint8_t*) dst
+ r
->w_fz
* o_index
, (uint8_t*) src
+ r
->w_fz
* i_index
, (int) r
->w_fz
);
1602 pa_memblock_release(input
->memblock
);
1603 pa_memblock_release(output
->memblock
);
1605 *out_n_frames
= o_index
;
1607 trivial_data
->i_counter
+= in_n_frames
;
1609 /* Normalize counters */
1610 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1611 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1613 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1614 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1620 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1621 struct trivial_data
*trivial_data
;
1624 trivial_data
= r
->impl
.data
;
1626 trivial_data
->i_counter
= 0;
1627 trivial_data
->o_counter
= 0;
1630 static int trivial_init(pa_resampler
*r
) {
1631 struct trivial_data
*trivial_data
;
1634 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1636 r
->impl
.resample
= trivial_resample
;
1637 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1638 r
->impl
.reset
= trivial_update_rates_or_reset
;
1639 r
->impl
.data
= trivial_data
;
1644 /* Peak finder implementation */
1646 static unsigned peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1647 unsigned c
, o_index
= 0;
1648 unsigned i
, i_end
= 0;
1650 struct peaks_data
*peaks_data
;
1655 pa_assert(out_n_frames
);
1657 peaks_data
= r
->impl
.data
;
1658 src
= pa_memblock_acquire_chunk(input
);
1659 dst
= pa_memblock_acquire_chunk(output
);
1661 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1662 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1664 while (i_end
< in_n_frames
) {
1665 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1666 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1668 pa_assert_fp(o_index
* r
->w_fz
< pa_memblock_get_length(output
->memblock
));
1670 /* 1ch float is treated separately, because that is the common case */
1671 if (r
->work_channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1672 float *s
= (float*) src
+ i
;
1673 float *d
= (float*) dst
+ o_index
;
1675 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1676 float n
= fabsf(*s
++);
1678 if (n
> peaks_data
->max_f
[0])
1679 peaks_data
->max_f
[0] = n
;
1683 *d
= peaks_data
->max_f
[0];
1684 peaks_data
->max_f
[0] = 0;
1685 o_index
++, peaks_data
->o_counter
++;
1687 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1688 int16_t *s
= (int16_t*) src
+ r
->work_channels
* i
;
1689 int16_t *d
= (int16_t*) dst
+ r
->work_channels
* o_index
;
1691 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1692 for (c
= 0; c
< r
->work_channels
; c
++) {
1693 int16_t n
= abs(*s
++);
1695 if (n
> peaks_data
->max_i
[c
])
1696 peaks_data
->max_i
[c
] = n
;
1700 for (c
= 0; c
< r
->work_channels
; c
++, d
++) {
1701 *d
= peaks_data
->max_i
[c
];
1702 peaks_data
->max_i
[c
] = 0;
1704 o_index
++, peaks_data
->o_counter
++;
1707 float *s
= (float*) src
+ r
->work_channels
* i
;
1708 float *d
= (float*) dst
+ r
->work_channels
* o_index
;
1710 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1711 for (c
= 0; c
< r
->work_channels
; c
++) {
1712 float n
= fabsf(*s
++);
1714 if (n
> peaks_data
->max_f
[c
])
1715 peaks_data
->max_f
[c
] = n
;
1719 for (c
= 0; c
< r
->work_channels
; c
++, d
++) {
1720 *d
= peaks_data
->max_f
[c
];
1721 peaks_data
->max_f
[c
] = 0;
1723 o_index
++, peaks_data
->o_counter
++;
1728 pa_memblock_release(input
->memblock
);
1729 pa_memblock_release(output
->memblock
);
1731 *out_n_frames
= o_index
;
1733 peaks_data
->i_counter
+= in_n_frames
;
1735 /* Normalize counters */
1736 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1737 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1739 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1740 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1746 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1747 struct peaks_data
*peaks_data
;
1750 peaks_data
= r
->impl
.data
;
1752 peaks_data
->i_counter
= 0;
1753 peaks_data
->o_counter
= 0;
1756 static int peaks_init(pa_resampler
*r
) {
1757 struct peaks_data
*peaks_data
;
1759 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1760 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1762 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1764 r
->impl
.resample
= peaks_resample
;
1765 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1766 r
->impl
.reset
= peaks_update_rates_or_reset
;
1767 r
->impl
.data
= peaks_data
;
1772 /*** ffmpeg based implementation ***/
1774 static unsigned ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1775 unsigned used_frames
= 0, c
;
1776 int previous_consumed_frames
= -1;
1777 struct ffmpeg_data
*ffmpeg_data
;
1782 pa_assert(out_n_frames
);
1784 ffmpeg_data
= r
->impl
.data
;
1786 for (c
= 0; c
< r
->work_channels
; c
++) {
1789 int16_t *p
, *t
, *k
, *q
, *s
;
1790 int consumed_frames
;
1792 /* Allocate a new block */
1793 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1794 p
= pa_memblock_acquire(b
);
1796 /* Now copy the input data, splitting up channels */
1797 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1799 for (u
= 0; u
< in_n_frames
; u
++) {
1801 t
+= r
->work_channels
;
1804 pa_memblock_release(input
->memblock
);
1806 /* Allocate buffer for the result */
1807 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1808 q
= pa_memblock_acquire(w
);
1811 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1814 (int) in_n_frames
, (int) *out_n_frames
,
1815 c
>= (unsigned) (r
->work_channels
-1));
1817 pa_memblock_release(b
);
1818 pa_memblock_unref(b
);
1820 pa_assert(consumed_frames
<= (int) in_n_frames
);
1821 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1822 previous_consumed_frames
= consumed_frames
;
1824 /* And place the results in the output buffer */
1825 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1826 for (u
= 0; u
< used_frames
; u
++) {
1829 s
+= r
->work_channels
;
1831 pa_memblock_release(output
->memblock
);
1832 pa_memblock_release(w
);
1833 pa_memblock_unref(w
);
1836 *out_n_frames
= used_frames
;
1838 return in_n_frames
- previous_consumed_frames
;
1841 static void ffmpeg_free(pa_resampler
*r
) {
1843 struct ffmpeg_data
*ffmpeg_data
;
1847 ffmpeg_data
= r
->impl
.data
;
1848 if (ffmpeg_data
->state
)
1849 av_resample_close(ffmpeg_data
->state
);
1851 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1852 if (ffmpeg_data
->buf
[c
].memblock
)
1853 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1856 static int ffmpeg_init(pa_resampler
*r
) {
1858 struct ffmpeg_data
*ffmpeg_data
;
1862 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1864 /* We could probably implement different quality levels by
1865 * adjusting the filter parameters here. However, ffmpeg
1866 * internally only uses these hardcoded values, so let's use them
1867 * here for now as well until ffmpeg makes this configurable. */
1869 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1872 r
->impl
.free
= ffmpeg_free
;
1873 r
->impl
.resample
= ffmpeg_resample
;
1874 r
->impl
.data
= (void *) ffmpeg_data
;
1876 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1877 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1882 /*** copy (noop) implementation ***/
1884 static int copy_init(pa_resampler
*r
) {
1887 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);