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
);
82 struct { /* data specific to the trivial resampler */
87 struct { /* data specific to the peak finder pseudo resampler */
91 float max_f
[PA_CHANNELS_MAX
];
92 int16_t max_i
[PA_CHANNELS_MAX
];
96 #ifdef HAVE_LIBSAMPLERATE
97 struct { /* data specific to libsamplerate */
103 struct { /* data specific to speex */
104 SpeexResamplerState
* state
;
108 struct { /* data specific to ffmpeg */
109 struct AVResampleContext
*state
;
110 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 pa_resampler
* pa_resampler_new(
197 const pa_sample_spec
*a
,
198 const pa_channel_map
*am
,
199 const pa_sample_spec
*b
,
200 const pa_channel_map
*bm
,
201 pa_resample_method_t method
,
202 pa_resample_flags_t flags
) {
204 pa_resampler
*r
= NULL
;
209 pa_assert(pa_sample_spec_valid(a
));
210 pa_assert(pa_sample_spec_valid(b
));
211 pa_assert(method
>= 0);
212 pa_assert(method
< PA_RESAMPLER_MAX
);
216 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && a
->rate
== b
->rate
) {
217 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
218 method
= PA_RESAMPLER_COPY
;
221 if (!pa_resample_method_supported(method
)) {
222 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
223 method
= PA_RESAMPLER_AUTO
;
226 if (method
== PA_RESAMPLER_FFMPEG
&& (flags
& PA_RESAMPLER_VARIABLE_RATE
)) {
227 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
228 method
= PA_RESAMPLER_AUTO
;
231 if (method
== PA_RESAMPLER_COPY
&& ((flags
& PA_RESAMPLER_VARIABLE_RATE
) || a
->rate
!= b
->rate
)) {
232 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
233 method
= PA_RESAMPLER_AUTO
;
236 if (method
== PA_RESAMPLER_AUTO
) {
238 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
240 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
241 method
= PA_RESAMPLER_TRIVIAL
;
243 method
= PA_RESAMPLER_FFMPEG
;
247 r
= pa_xnew0(pa_resampler
, 1);
252 /* Fill sample specs */
256 /* set up the remap structure */
257 r
->remap
.i_ss
= &r
->i_ss
;
258 r
->remap
.o_ss
= &r
->o_ss
;
259 r
->remap
.format
= &r
->work_format
;
263 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
268 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
271 r
->i_fz
= pa_frame_size(a
);
272 r
->o_fz
= pa_frame_size(b
);
276 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
278 if ((method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
279 (method
== PA_RESAMPLER_FFMPEG
))
280 r
->work_format
= PA_SAMPLE_S16NE
;
281 else if (method
== PA_RESAMPLER_TRIVIAL
|| method
== PA_RESAMPLER_COPY
|| method
== PA_RESAMPLER_PEAKS
) {
283 if (r
->map_required
|| a
->format
!= b
->format
|| method
== PA_RESAMPLER_PEAKS
) {
285 if (a
->format
== PA_SAMPLE_S16NE
|| b
->format
== PA_SAMPLE_S16NE
)
286 r
->work_format
= PA_SAMPLE_S16NE
;
287 else if (a
->format
== PA_SAMPLE_S32NE
|| a
->format
== PA_SAMPLE_S32RE
||
288 a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
||
289 a
->format
== PA_SAMPLE_S24NE
|| a
->format
== PA_SAMPLE_S24RE
||
290 a
->format
== PA_SAMPLE_S24_32NE
|| a
->format
== PA_SAMPLE_S24_32RE
||
291 b
->format
== PA_SAMPLE_S32NE
|| b
->format
== PA_SAMPLE_S32RE
||
292 b
->format
== PA_SAMPLE_FLOAT32NE
|| b
->format
== PA_SAMPLE_FLOAT32RE
||
293 b
->format
== PA_SAMPLE_S24NE
|| b
->format
== PA_SAMPLE_S24RE
||
294 b
->format
== PA_SAMPLE_S24_32NE
|| b
->format
== PA_SAMPLE_S24_32RE
)
295 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
297 r
->work_format
= PA_SAMPLE_S16NE
;
300 r
->work_format
= a
->format
;
303 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
305 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
307 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
309 if (r
->i_ss
.format
!= r
->work_format
) {
310 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
311 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
314 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
315 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
320 if (r
->o_ss
.format
!= r
->work_format
) {
321 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
322 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
325 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
326 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
331 /* initialize implementation */
332 if (init_table
[method
](r
) < 0)
343 void pa_resampler_free(pa_resampler
*r
) {
349 if (r
->to_work_format_buf
.memblock
)
350 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
351 if (r
->remap_buf
.memblock
)
352 pa_memblock_unref(r
->remap_buf
.memblock
);
353 if (r
->resample_buf
.memblock
)
354 pa_memblock_unref(r
->resample_buf
.memblock
);
355 if (r
->from_work_format_buf
.memblock
)
356 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
361 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
365 if (r
->i_ss
.rate
== rate
)
370 r
->impl_update_rates(r
);
373 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
377 if (r
->o_ss
.rate
== rate
)
382 r
->impl_update_rates(r
);
385 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
388 /* Let's round up here to make it more likely that the caller will get at
389 * least out_length amount of data from pa_resampler_run().
391 * We don't take the leftover into account here. If we did, then it might
392 * be in theory possible that this function would return 0 and
393 * pa_resampler_run() would also return 0. That could lead to infinite
394 * loops. When the leftover is ignored here, such loops would eventually
395 * terminate, because the leftover would grow each round, finally
396 * surpassing the minimum input threshold of the resampler. */
397 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
;
400 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
405 /* Let's round up here to ensure that the caller will always allocate big
406 * enough output buffer. */
408 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
410 if (r
->remap_buf_contains_leftover_data
)
411 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
413 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
416 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
417 size_t block_size_max
;
418 pa_sample_spec max_ss
;
424 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
426 /* We deduce the "largest" sample spec we're using during the
428 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
430 /* We silently assume that the format enum is ordered by size */
431 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
432 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
434 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
436 max_fs
= pa_frame_size(&max_ss
);
437 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
439 if (r
->remap_buf_contains_leftover_data
)
440 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
442 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
445 void pa_resampler_reset(pa_resampler
*r
) {
451 r
->remap_buf_contains_leftover_data
= false;
454 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
460 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
466 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
472 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
478 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
484 static const char * const resample_methods
[] = {
485 "src-sinc-best-quality",
486 "src-sinc-medium-quality",
488 "src-zero-order-hold",
519 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
521 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
524 return resample_methods
[m
];
527 int pa_resample_method_supported(pa_resample_method_t m
) {
529 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
532 #ifndef HAVE_LIBSAMPLERATE
533 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
538 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
540 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
547 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
548 pa_resample_method_t m
;
552 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
553 if (pa_streq(string
, resample_methods
[m
]))
556 if (pa_streq(string
, "speex-fixed"))
557 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
559 if (pa_streq(string
, "speex-float"))
560 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
562 return PA_RESAMPLER_INVALID
;
565 static bool on_left(pa_channel_position_t p
) {
568 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
569 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
570 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
571 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
572 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
573 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
576 static bool on_right(pa_channel_position_t p
) {
579 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
580 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
581 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
582 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
583 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
584 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
587 static bool on_center(pa_channel_position_t p
) {
590 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
591 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
592 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
593 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
594 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
597 static bool on_lfe(pa_channel_position_t p
) {
599 p
== PA_CHANNEL_POSITION_LFE
;
602 static bool on_front(pa_channel_position_t p
) {
604 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
605 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
606 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
607 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
608 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
609 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
610 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
611 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
614 static bool on_rear(pa_channel_position_t p
) {
616 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
617 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
618 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
619 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
620 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
621 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
624 static bool on_side(pa_channel_position_t p
) {
626 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
627 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
628 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
638 static int front_rear_side(pa_channel_position_t p
) {
648 static void calc_map_table(pa_resampler
*r
) {
651 bool ic_connected
[PA_CHANNELS_MAX
];
659 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
)))))
664 n_oc
= r
->o_ss
.channels
;
665 n_ic
= r
->i_ss
.channels
;
667 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
668 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
670 memset(ic_connected
, 0, sizeof(ic_connected
));
671 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
673 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
676 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
677 m
->map_table_f
[oc
][oc
] = 1.0f
;
679 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
681 for (oc
= 0; oc
< n_oc
; oc
++) {
682 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
684 for (ic
= 0; ic
< n_ic
; ic
++) {
685 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
687 /* We shall not do any remixing. Hence, just check by name */
689 m
->map_table_f
[oc
][ic
] = 1.0f
;
694 /* OK, we shall do the full monty: upmixing and downmixing. Our
695 * algorithm is relatively simple, does not do spacialization, delay
696 * elements or apply lowpass filters for LFE. Patches are always
697 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
698 * probably wouldn't make any sense anyway.)
700 * This code is not idempotent: downmixing an upmixed stereo stream is
701 * not identical to the original. The volume will not match, and the
702 * two channels will be a linear combination of both.
704 * This is loosely based on random suggestions found on the Internet,
706 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
709 * The algorithm works basically like this:
711 * 1) Connect all channels with matching names.
714 * S:Mono: Copy into all D:channels
715 * D:Mono: Avg all S:channels
717 * 3) Mix D:Left, D:Right:
718 * D:Left: If not connected, avg all S:Left
719 * D:Right: If not connected, avg all S:Right
722 * If not connected, avg all S:Center
723 * If still not connected, avg all S:Left, S:Right
726 * If not connected, avg all S:*
728 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
729 * connected, mix into all D:left and all D:right channels. Gain is
732 * 7) Make sure S:Center, S:LFE is used:
734 * S:Center, S:LFE: If not connected, mix into all D:left, all
735 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
736 * for LFE. C-front is only mixed into L-front/R-front if available,
737 * otherwise into all L/R channels. Similarly for C-rear.
739 * 8) Normalize each row in the matrix such that the sum for each row is
740 * not larger than 1.0 in order to avoid clipping.
742 * S: and D: shall relate to the source resp. destination channels.
744 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
745 * rear if needed. For 4: we try to find some suitable C source for C,
746 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
747 * channels. For 6: the rear channels should not be dropped entirely,
748 * however have only minimal impact. For 7: movies usually encode
749 * speech on the center channel. Thus we have to make sure this channel
750 * is distributed to L and R if not available in the output. Also, LFE
751 * is used to achieve a greater dynamic range, and thus we should try
752 * to do our best to pass it to L+R.
759 ic_unconnected_left
= 0,
760 ic_unconnected_right
= 0,
761 ic_unconnected_center
= 0,
762 ic_unconnected_lfe
= 0;
763 bool ic_unconnected_center_mixed_in
= 0;
767 for (ic
= 0; ic
< n_ic
; ic
++) {
768 if (on_left(r
->i_cm
.map
[ic
]))
770 if (on_right(r
->i_cm
.map
[ic
]))
772 if (on_center(r
->i_cm
.map
[ic
]))
776 for (oc
= 0; oc
< n_oc
; oc
++) {
777 bool oc_connected
= false;
778 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
780 for (ic
= 0; ic
< n_ic
; ic
++) {
781 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
783 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
784 m
->map_table_f
[oc
][ic
] = 1.0f
;
787 ic_connected
[ic
] = true;
789 else if (b
== PA_CHANNEL_POSITION_MONO
) {
790 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
793 ic_connected
[ic
] = true;
798 /* Try to find matching input ports for this output port */
802 /* We are not connected and on the left side, let's
803 * average all left side input channels. */
806 for (ic
= 0; ic
< n_ic
; ic
++)
807 if (on_left(r
->i_cm
.map
[ic
])) {
808 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
809 ic_connected
[ic
] = true;
812 /* We ignore the case where there is no left input channel.
813 * Something is really wrong in this case anyway. */
815 } else if (on_right(b
)) {
817 /* We are not connected and on the right side, let's
818 * average all right side input channels. */
821 for (ic
= 0; ic
< n_ic
; ic
++)
822 if (on_right(r
->i_cm
.map
[ic
])) {
823 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
824 ic_connected
[ic
] = true;
827 /* We ignore the case where there is no right input
828 * channel. Something is really wrong in this case anyway.
831 } else if (on_center(b
)) {
835 /* We are not connected and at the center. Let's average
836 * all center input channels. */
838 for (ic
= 0; ic
< n_ic
; ic
++)
839 if (on_center(r
->i_cm
.map
[ic
])) {
840 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
841 ic_connected
[ic
] = true;
844 } else if (ic_left
+ ic_right
> 0) {
846 /* Hmm, no center channel around, let's synthesize it
847 * by mixing L and R.*/
849 for (ic
= 0; ic
< n_ic
; ic
++)
850 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
851 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
852 ic_connected
[ic
] = true;
856 /* We ignore the case where there is not even a left or
857 * right input channel. Something is really wrong in this
860 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
862 /* We are not connected and an LFE. Let's average all
863 * channels for LFE. */
865 for (ic
= 0; ic
< n_ic
; ic
++)
866 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
868 /* Please note that a channel connected to LFE doesn't
869 * really count as connected. */
874 for (ic
= 0; ic
< n_ic
; ic
++) {
875 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
877 if (ic_connected
[ic
])
881 ic_unconnected_left
++;
882 else if (on_right(a
))
883 ic_unconnected_right
++;
884 else if (on_center(a
))
885 ic_unconnected_center
++;
887 ic_unconnected_lfe
++;
890 for (ic
= 0; ic
< n_ic
; ic
++) {
891 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
893 if (ic_connected
[ic
])
896 for (oc
= 0; oc
< n_oc
; oc
++) {
897 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
899 if (on_left(a
) && on_left(b
))
900 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
902 else if (on_right(a
) && on_right(b
))
903 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
905 else if (on_center(a
) && on_center(b
)) {
906 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
907 ic_unconnected_center_mixed_in
= true;
909 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
910 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
914 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
915 unsigned ncenter
[PA_CHANNELS_MAX
];
916 bool found_frs
[PA_CHANNELS_MAX
];
918 memset(ncenter
, 0, sizeof(ncenter
));
919 memset(found_frs
, 0, sizeof(found_frs
));
921 /* Hmm, as it appears there was no center channel we
922 could mix our center channel in. In this case, mix it into
923 left and right. Using .5 as the factor. */
925 for (ic
= 0; ic
< n_ic
; ic
++) {
927 if (ic_connected
[ic
])
930 if (!on_center(r
->i_cm
.map
[ic
]))
933 for (oc
= 0; oc
< n_oc
; oc
++) {
935 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
938 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
939 found_frs
[ic
] = true;
944 for (oc
= 0; oc
< n_oc
; oc
++) {
946 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
949 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
954 for (oc
= 0; oc
< n_oc
; oc
++) {
956 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
959 if (ncenter
[oc
] <= 0)
962 for (ic
= 0; ic
< n_ic
; ic
++) {
964 if (!on_center(r
->i_cm
.map
[ic
]))
967 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
968 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
974 for (oc
= 0; oc
< n_oc
; oc
++) {
976 for (ic
= 0; ic
< n_ic
; ic
++)
977 sum
+= m
->map_table_f
[oc
][ic
];
980 for (ic
= 0; ic
< n_ic
; ic
++)
981 m
->map_table_f
[oc
][ic
] /= sum
;
984 /* make an 16:16 int version of the matrix */
985 for (oc
= 0; oc
< n_oc
; oc
++)
986 for (ic
= 0; ic
< n_ic
; ic
++)
987 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
991 pa_strbuf_printf(s
, " ");
992 for (ic
= 0; ic
< n_ic
; ic
++)
993 pa_strbuf_printf(s
, " I%02u ", ic
);
994 pa_strbuf_puts(s
, "\n +");
996 for (ic
= 0; ic
< n_ic
; ic
++)
997 pa_strbuf_printf(s
, "------");
998 pa_strbuf_puts(s
, "\n");
1000 for (oc
= 0; oc
< n_oc
; oc
++) {
1001 pa_strbuf_printf(s
, "O%02u |", oc
);
1003 for (ic
= 0; ic
< n_ic
; ic
++)
1004 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1006 pa_strbuf_puts(s
, "\n");
1009 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1012 /* initialize the remapping function */
1016 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1022 pa_assert(input
->memblock
);
1024 /* Convert the incoming sample into the work sample format and place them
1025 * in to_work_format_buf. */
1027 if (!r
->to_work_format_func
|| !input
->length
)
1030 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1032 r
->to_work_format_buf
.index
= 0;
1033 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1035 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1036 if (r
->to_work_format_buf
.memblock
)
1037 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1039 r
->to_work_format_buf_samples
= n_samples
;
1040 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1043 src
= pa_memblock_acquire_chunk(input
);
1044 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1046 r
->to_work_format_func(n_samples
, src
, dst
);
1048 pa_memblock_release(input
->memblock
);
1049 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1051 return &r
->to_work_format_buf
;
1054 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1055 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1057 size_t leftover_length
= 0;
1062 pa_assert(input
->memblock
);
1064 /* Remap channels and place the result in remap_buf. There may be leftover
1065 * data in the beginning of remap_buf. The leftover data is already
1066 * remapped, so it's not part of the input, it's part of the output. */
1068 have_leftover
= r
->remap_buf_contains_leftover_data
;
1069 r
->remap_buf_contains_leftover_data
= false;
1071 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1073 else if (input
->length
<= 0)
1074 return &r
->remap_buf
;
1076 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1077 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1079 if (have_leftover
) {
1080 leftover_length
= r
->remap_buf
.length
;
1081 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1084 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1085 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1087 if (have_leftover
) {
1088 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1089 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1091 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1092 dst
= pa_memblock_acquire(new_block
);
1093 memcpy(dst
, src
, leftover_length
);
1094 pa_memblock_release(r
->remap_buf
.memblock
);
1095 pa_memblock_release(new_block
);
1097 pa_memblock_unref(r
->remap_buf
.memblock
);
1098 r
->remap_buf
.memblock
= new_block
;
1099 r
->remap_buf_size
= r
->remap_buf
.length
;
1103 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1104 if (r
->remap_buf
.memblock
)
1105 pa_memblock_unref(r
->remap_buf
.memblock
);
1107 r
->remap_buf_size
= r
->remap_buf
.length
;
1108 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1112 src
= pa_memblock_acquire_chunk(input
);
1113 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1115 if (r
->map_required
) {
1116 pa_remap_t
*remap
= &r
->remap
;
1118 pa_assert(remap
->do_remap
);
1119 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1122 memcpy(dst
, src
, input
->length
);
1124 pa_memblock_release(input
->memblock
);
1125 pa_memblock_release(r
->remap_buf
.memblock
);
1127 return &r
->remap_buf
;
1130 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1131 unsigned in_n_frames
, in_n_samples
;
1132 unsigned out_n_frames
, out_n_samples
;
1137 /* Resample the data and place the result in resample_buf. */
1139 if (!r
->impl_resample
|| !input
->length
)
1142 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1143 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1145 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1146 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1148 r
->resample_buf
.index
= 0;
1149 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1151 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1152 if (r
->resample_buf
.memblock
)
1153 pa_memblock_unref(r
->resample_buf
.memblock
);
1155 r
->resample_buf_samples
= out_n_samples
;
1156 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1159 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1160 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1162 return &r
->resample_buf
;
1165 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1166 unsigned n_samples
, n_frames
;
1172 /* Convert the data into the correct sample type and place the result in
1173 * from_work_format_buf. */
1175 if (!r
->from_work_format_func
|| !input
->length
)
1178 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1179 n_frames
= n_samples
/ r
->o_ss
.channels
;
1181 r
->from_work_format_buf
.index
= 0;
1182 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1184 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1185 if (r
->from_work_format_buf
.memblock
)
1186 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1188 r
->from_work_format_buf_samples
= n_samples
;
1189 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1192 src
= pa_memblock_acquire_chunk(input
);
1193 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1194 r
->from_work_format_func(n_samples
, src
, dst
);
1195 pa_memblock_release(input
->memblock
);
1196 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1198 return &r
->from_work_format_buf
;
1201 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1207 pa_assert(in
->length
);
1208 pa_assert(in
->memblock
);
1209 pa_assert(in
->length
% r
->i_fz
== 0);
1211 buf
= (pa_memchunk
*) in
;
1212 buf
= convert_to_work_format(r
, buf
);
1213 buf
= remap_channels(r
, buf
);
1214 buf
= resample(r
, buf
);
1217 buf
= convert_from_work_format(r
, buf
);
1221 pa_memblock_ref(buf
->memblock
);
1223 pa_memchunk_reset(buf
);
1225 pa_memchunk_reset(out
);
1228 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1235 /* Store the leftover to remap_buf. */
1237 r
->remap_buf
.length
= len
;
1239 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1240 if (r
->remap_buf
.memblock
)
1241 pa_memblock_unref(r
->remap_buf
.memblock
);
1243 r
->remap_buf_size
= r
->remap_buf
.length
;
1244 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1247 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1248 memcpy(dst
, buf
, r
->remap_buf
.length
);
1249 pa_memblock_release(r
->remap_buf
.memblock
);
1251 r
->remap_buf_contains_leftover_data
= true;
1254 /*** libsamplerate based implementation ***/
1256 #ifdef HAVE_LIBSAMPLERATE
1257 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1263 pa_assert(out_n_frames
);
1265 memset(&data
, 0, sizeof(data
));
1267 data
.data_in
= pa_memblock_acquire_chunk(input
);
1268 data
.input_frames
= (long int) in_n_frames
;
1270 data
.data_out
= pa_memblock_acquire_chunk(output
);
1271 data
.output_frames
= (long int) *out_n_frames
;
1273 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1274 data
.end_of_input
= 0;
1276 pa_assert_se(src_process(r
->src
.state
, &data
) == 0);
1278 if (data
.input_frames_used
< in_n_frames
) {
1279 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1280 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1282 save_leftover(r
, leftover_data
, leftover_length
);
1285 pa_memblock_release(input
->memblock
);
1286 pa_memblock_release(output
->memblock
);
1288 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1291 static void libsamplerate_update_rates(pa_resampler
*r
) {
1294 pa_assert_se(src_set_ratio(r
->src
.state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1297 static void libsamplerate_reset(pa_resampler
*r
) {
1300 pa_assert_se(src_reset(r
->src
.state
) == 0);
1303 static void libsamplerate_free(pa_resampler
*r
) {
1307 src_delete(r
->src
.state
);
1310 static int libsamplerate_init(pa_resampler
*r
) {
1315 if (!(r
->src
.state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1318 r
->impl_free
= libsamplerate_free
;
1319 r
->impl_update_rates
= libsamplerate_update_rates
;
1320 r
->impl_resample
= libsamplerate_resample
;
1321 r
->impl_reset
= libsamplerate_reset
;
1328 /*** speex based implementation ***/
1330 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1332 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1337 pa_assert(out_n_frames
);
1339 in
= pa_memblock_acquire_chunk(input
);
1340 out
= pa_memblock_acquire_chunk(output
);
1342 pa_assert_se(speex_resampler_process_interleaved_float(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
1344 pa_memblock_release(input
->memblock
);
1345 pa_memblock_release(output
->memblock
);
1347 pa_assert(inf
== in_n_frames
);
1348 *out_n_frames
= outf
;
1351 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1353 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1358 pa_assert(out_n_frames
);
1360 in
= pa_memblock_acquire_chunk(input
);
1361 out
= pa_memblock_acquire_chunk(output
);
1363 pa_assert_se(speex_resampler_process_interleaved_int(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
1365 pa_memblock_release(input
->memblock
);
1366 pa_memblock_release(output
->memblock
);
1368 pa_assert(inf
== in_n_frames
);
1369 *out_n_frames
= outf
;
1372 static void speex_update_rates(pa_resampler
*r
) {
1375 pa_assert_se(speex_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1378 static void speex_reset(pa_resampler
*r
) {
1381 pa_assert_se(speex_resampler_reset_mem(r
->speex
.state
) == 0);
1384 static void speex_free(pa_resampler
*r
) {
1387 if (!r
->speex
.state
)
1390 speex_resampler_destroy(r
->speex
.state
);
1393 static int speex_init(pa_resampler
*r
) {
1398 r
->impl_free
= speex_free
;
1399 r
->impl_update_rates
= speex_update_rates
;
1400 r
->impl_reset
= speex_reset
;
1402 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1404 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1405 r
->impl_resample
= speex_resample_int
;
1408 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1410 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1411 r
->impl_resample
= speex_resample_float
;
1414 pa_log_info("Choosing speex quality setting %i.", q
);
1416 if (!(r
->speex
.state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1423 /* Trivial implementation */
1425 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1427 unsigned i_index
, o_index
;
1433 pa_assert(out_n_frames
);
1435 fz
= r
->w_sz
* r
->o_ss
.channels
;
1437 src
= pa_memblock_acquire_chunk(input
);
1438 dst
= pa_memblock_acquire_chunk(output
);
1440 for (o_index
= 0;; o_index
++, r
->trivial
.o_counter
++) {
1441 i_index
= ((uint64_t) r
->trivial
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1442 i_index
= i_index
> r
->trivial
.i_counter
? i_index
- r
->trivial
.i_counter
: 0;
1444 if (i_index
>= in_n_frames
)
1447 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1449 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1452 pa_memblock_release(input
->memblock
);
1453 pa_memblock_release(output
->memblock
);
1455 *out_n_frames
= o_index
;
1457 r
->trivial
.i_counter
+= in_n_frames
;
1459 /* Normalize counters */
1460 while (r
->trivial
.i_counter
>= r
->i_ss
.rate
) {
1461 pa_assert(r
->trivial
.o_counter
>= r
->o_ss
.rate
);
1463 r
->trivial
.i_counter
-= r
->i_ss
.rate
;
1464 r
->trivial
.o_counter
-= r
->o_ss
.rate
;
1468 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1471 r
->trivial
.i_counter
= 0;
1472 r
->trivial
.o_counter
= 0;
1475 static int trivial_init(pa_resampler
*r
) {
1478 r
->trivial
.o_counter
= r
->trivial
.i_counter
= 0;
1480 r
->impl_resample
= trivial_resample
;
1481 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1482 r
->impl_reset
= trivial_update_rates_or_reset
;
1487 /* Peak finder implementation */
1489 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1490 unsigned c
, o_index
= 0;
1491 unsigned i
, i_end
= 0;
1497 pa_assert(out_n_frames
);
1499 src
= pa_memblock_acquire_chunk(input
);
1500 dst
= pa_memblock_acquire_chunk(output
);
1502 i
= ((uint64_t) r
->peaks
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1503 i
= i
> r
->peaks
.i_counter
? i
- r
->peaks
.i_counter
: 0;
1505 while (i_end
< in_n_frames
) {
1506 i_end
= ((uint64_t) (r
->peaks
.o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1507 i_end
= i_end
> r
->peaks
.i_counter
? i_end
- r
->peaks
.i_counter
: 0;
1509 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1511 /* 1ch float is treated separately, because that is the common case */
1512 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1513 float *s
= (float*) src
+ i
;
1514 float *d
= (float*) dst
+ o_index
;
1516 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1517 float n
= fabsf(*s
++);
1519 if (n
> r
->peaks
.max_f
[0])
1520 r
->peaks
.max_f
[0] = n
;
1524 *d
= r
->peaks
.max_f
[0];
1525 r
->peaks
.max_f
[0] = 0;
1526 o_index
++, r
->peaks
.o_counter
++;
1528 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1529 int16_t *s
= (int16_t*) src
+ r
->i_ss
.channels
* i
;
1530 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1532 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1533 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1534 int16_t n
= abs(*s
++);
1536 if (n
> r
->peaks
.max_i
[c
])
1537 r
->peaks
.max_i
[c
] = n
;
1541 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1542 *d
= r
->peaks
.max_i
[c
];
1543 r
->peaks
.max_i
[c
] = 0;
1545 o_index
++, r
->peaks
.o_counter
++;
1548 float *s
= (float*) src
+ r
->i_ss
.channels
* i
;
1549 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1551 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1552 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1553 float n
= fabsf(*s
++);
1555 if (n
> r
->peaks
.max_f
[c
])
1556 r
->peaks
.max_f
[c
] = n
;
1560 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1561 *d
= r
->peaks
.max_f
[c
];
1562 r
->peaks
.max_f
[c
] = 0;
1564 o_index
++, r
->peaks
.o_counter
++;
1569 pa_memblock_release(input
->memblock
);
1570 pa_memblock_release(output
->memblock
);
1572 *out_n_frames
= o_index
;
1574 r
->peaks
.i_counter
+= in_n_frames
;
1576 /* Normalize counters */
1577 while (r
->peaks
.i_counter
>= r
->i_ss
.rate
) {
1578 pa_assert(r
->peaks
.o_counter
>= r
->o_ss
.rate
);
1580 r
->peaks
.i_counter
-= r
->i_ss
.rate
;
1581 r
->peaks
.o_counter
-= r
->o_ss
.rate
;
1585 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1588 r
->peaks
.i_counter
= 0;
1589 r
->peaks
.o_counter
= 0;
1592 static int peaks_init(pa_resampler
*r
) {
1594 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1595 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1597 r
->peaks
.o_counter
= r
->peaks
.i_counter
= 0;
1598 memset(r
->peaks
.max_i
, 0, sizeof(r
->peaks
.max_i
));
1599 memset(r
->peaks
.max_f
, 0, sizeof(r
->peaks
.max_f
));
1601 r
->impl_resample
= peaks_resample
;
1602 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1603 r
->impl_reset
= peaks_update_rates_or_reset
;
1608 /*** ffmpeg based implementation ***/
1610 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1611 unsigned used_frames
= 0, c
;
1612 int previous_consumed_frames
= -1;
1617 pa_assert(out_n_frames
);
1619 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1622 int16_t *p
, *t
, *k
, *q
, *s
;
1623 int consumed_frames
;
1625 /* Allocate a new block */
1626 b
= pa_memblock_new(r
->mempool
, r
->ffmpeg
.buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1627 p
= pa_memblock_acquire(b
);
1629 /* Now copy the input data, splitting up channels */
1630 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1632 for (u
= 0; u
< in_n_frames
; u
++) {
1634 t
+= r
->o_ss
.channels
;
1637 pa_memblock_release(input
->memblock
);
1639 /* Allocate buffer for the result */
1640 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1641 q
= pa_memblock_acquire(w
);
1644 used_frames
= (unsigned) av_resample(r
->ffmpeg
.state
,
1647 (int) in_n_frames
, (int) *out_n_frames
,
1648 c
>= (unsigned) (r
->o_ss
.channels
-1));
1650 pa_memblock_release(b
);
1651 pa_memblock_unref(b
);
1653 pa_assert(consumed_frames
<= (int) in_n_frames
);
1654 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1655 previous_consumed_frames
= consumed_frames
;
1657 /* And place the results in the output buffer */
1658 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1659 for (u
= 0; u
< used_frames
; u
++) {
1662 s
+= r
->o_ss
.channels
;
1664 pa_memblock_release(output
->memblock
);
1665 pa_memblock_release(w
);
1666 pa_memblock_unref(w
);
1669 if (previous_consumed_frames
< (int) in_n_frames
) {
1670 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1671 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1673 save_leftover(r
, leftover_data
, leftover_length
);
1674 pa_memblock_release(input
->memblock
);
1677 *out_n_frames
= used_frames
;
1680 static void ffmpeg_free(pa_resampler
*r
) {
1685 if (r
->ffmpeg
.state
)
1686 av_resample_close(r
->ffmpeg
.state
);
1688 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
1689 if (r
->ffmpeg
.buf
[c
].memblock
)
1690 pa_memblock_unref(r
->ffmpeg
.buf
[c
].memblock
);
1693 static int ffmpeg_init(pa_resampler
*r
) {
1698 /* We could probably implement different quality levels by
1699 * adjusting the filter parameters here. However, ffmpeg
1700 * internally only uses these hardcoded values, so let's use them
1701 * here for now as well until ffmpeg makes this configurable. */
1703 if (!(r
->ffmpeg
.state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1706 r
->impl_free
= ffmpeg_free
;
1707 r
->impl_resample
= ffmpeg_resample
;
1709 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
1710 pa_memchunk_reset(&r
->ffmpeg
.buf
[c
]);
1715 /*** copy (noop) implementation ***/
1717 static int copy_init(pa_resampler
*r
) {
1720 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);