4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #if HAVE_LIBSAMPLERATE
31 #include <samplerate.h>
34 #include <liboil/liboilfuncs.h>
35 #include <liboil/liboil.h>
37 #include <pulse/xmalloc.h>
38 #include <pulsecore/sconv.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
42 #include "speexwrap.h"
44 #include "ffmpeg/avcodec.h"
46 #include "resampler.h"
49 pa_resample_method_t resample_method
;
50 pa_sample_spec i_ss
, o_ss
;
51 pa_channel_map i_cm
, o_cm
;
52 size_t i_fz
, o_fz
, w_sz
;
55 pa_memchunk buf1
, buf2
, buf3
, buf4
;
56 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
58 pa_sample_format_t work_format
;
60 pa_convert_func_t to_work_format_func
;
61 pa_convert_func_t from_work_format_func
;
63 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
66 void (*impl_free
)(pa_resampler
*r
);
67 void (*impl_update_rates
)(pa_resampler
*r
);
68 void (*impl_resample
)(pa_resampler
*r
, const pa_memchunk
*in
, unsigned in_samples
, pa_memchunk
*out
, unsigned *out_samples
);
70 struct { /* data specific to the trivial resampler */
75 #ifdef HAVE_LIBSAMPLERATE
76 struct { /* data specific to libsamplerate */
81 struct { /* data specific to speex */
82 SpeexResamplerState
* state
;
85 struct { /* data specific to ffmpeg */
86 struct AVResampleContext
*state
;
87 pa_memchunk buf
[PA_CHANNELS_MAX
];
91 static int trivial_init(pa_resampler
*r
);
92 static int speex_init(pa_resampler
*r
);
93 static int ffmpeg_init(pa_resampler
*r
);
94 #ifdef HAVE_LIBSAMPLERATE
95 static int libsamplerate_init(pa_resampler
*r
);
98 static void calc_map_table(pa_resampler
*r
);
100 static int (* const init_table
[])(pa_resampler
*r
) = {
101 #ifdef HAVE_LIBSAMPLERATE
102 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
103 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
104 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
105 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
106 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
108 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
109 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
110 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
111 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
112 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
114 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
115 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
116 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
117 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
118 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
119 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
120 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
121 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
122 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
123 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
124 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
125 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
126 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
127 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
128 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
129 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
130 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
131 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
132 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
133 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
134 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
135 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
136 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
137 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
138 [PA_RESAMPLER_AUTO
] = NULL
,
141 static inline size_t sample_size(pa_sample_format_t f
) {
142 pa_sample_spec ss
= {
148 return pa_sample_size(&ss
);
151 pa_resampler
* pa_resampler_new(
153 const pa_sample_spec
*a
,
154 const pa_channel_map
*am
,
155 const pa_sample_spec
*b
,
156 const pa_channel_map
*bm
,
157 pa_resample_method_t resample_method
,
160 pa_resampler
*r
= NULL
;
165 pa_assert(pa_sample_spec_valid(a
));
166 pa_assert(pa_sample_spec_valid(b
));
167 pa_assert(resample_method
>= 0);
168 pa_assert(resample_method
< PA_RESAMPLER_MAX
);
172 if (!pa_resample_method_supported(resample_method
)) {
173 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(resample_method
));
174 resample_method
= PA_RESAMPLER_AUTO
;
177 if (resample_method
== PA_RESAMPLER_FFMPEG
&& variable_rate
) {
178 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'." );
179 resample_method
= PA_RESAMPLER_AUTO
;
182 if (resample_method
== PA_RESAMPLER_AUTO
)
183 resample_method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 0;
185 r
= pa_xnew(pa_resampler
, 1);
187 r
->resample_method
= resample_method
;
190 r
->impl_update_rates
= NULL
;
191 r
->impl_resample
= NULL
;
193 /* Fill sample specs */
200 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
205 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
207 r
->i_fz
= pa_frame_size(a
);
208 r
->o_fz
= pa_frame_size(b
);
210 pa_memchunk_reset(&r
->buf1
);
211 pa_memchunk_reset(&r
->buf2
);
212 pa_memchunk_reset(&r
->buf3
);
213 pa_memchunk_reset(&r
->buf4
);
215 r
->buf1_samples
= r
->buf2_samples
= r
->buf3_samples
= r
->buf4_samples
= 0;
219 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(resample_method
));
221 if ((resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
222 (resample_method
== PA_RESAMPLER_FFMPEG
))
223 r
->work_format
= PA_SAMPLE_S16NE
;
224 else if (resample_method
== PA_RESAMPLER_TRIVIAL
) {
226 if (r
->map_required
|| a
->format
!= b
->format
) {
228 if (a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
)
229 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
231 r
->work_format
= PA_SAMPLE_S16NE
;
234 r
->work_format
= a
->format
;
237 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
239 r
->w_sz
= sample_size(r
->work_format
);
241 if (r
->i_ss
.format
== r
->work_format
)
242 r
->to_work_format_func
= NULL
;
243 else if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
244 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
247 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
248 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
252 if (r
->o_ss
.format
== r
->work_format
)
253 r
->from_work_format_func
= NULL
;
254 else if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
255 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
258 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
259 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
263 /* initialize implementation */
264 if (init_table
[resample_method
](r
) < 0)
276 void pa_resampler_free(pa_resampler
*r
) {
282 if (r
->buf1
.memblock
)
283 pa_memblock_unref(r
->buf1
.memblock
);
284 if (r
->buf2
.memblock
)
285 pa_memblock_unref(r
->buf2
.memblock
);
286 if (r
->buf3
.memblock
)
287 pa_memblock_unref(r
->buf3
.memblock
);
288 if (r
->buf4
.memblock
)
289 pa_memblock_unref(r
->buf4
.memblock
);
294 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
298 if (r
->i_ss
.rate
== rate
)
303 r
->impl_update_rates(r
);
306 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
310 if (r
->o_ss
.rate
== rate
)
315 r
->impl_update_rates(r
);
318 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
321 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
324 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
327 return r
->resample_method
;
330 static const char * const resample_methods
[] = {
331 "src-sinc-best-quality",
332 "src-sinc-medium-quality",
334 "src-zero-order-hold",
363 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
365 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
368 return resample_methods
[m
];
371 int pa_resample_method_supported(pa_resample_method_t m
) {
373 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
376 #ifndef HAVE_LIBSAMPLERATE
377 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
384 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
385 pa_resample_method_t m
;
389 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
390 if (!strcmp(string
, resample_methods
[m
]))
393 if (!strcmp(string
, "speex-fixed"))
394 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 0;
396 if (!strcmp(string
, "speex-float"))
397 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 0;
399 return PA_RESAMPLER_INVALID
;
402 static void calc_map_table(pa_resampler
*r
) {
407 if (!(r
->map_required
= (r
->i_ss
.channels
!= r
->o_ss
.channels
|| !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
))))
410 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
413 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
414 pa_channel_position_t a
, b
;
420 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
421 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
422 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
423 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
425 r
->map_table
[oc
][i
++] = ic
;
428 /* Add an end marker */
429 if (i
< PA_CHANNELS_MAX
)
430 r
->map_table
[oc
][i
] = -1;
434 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
440 pa_assert(input
->memblock
);
442 /* Convert the incoming sample into the work sample format and place them in buf1 */
444 if (!r
->to_work_format_func
|| !input
->length
)
447 n_samples
= (input
->length
/ r
->i_fz
) * r
->i_ss
.channels
;
450 r
->buf1
.length
= r
->w_sz
* n_samples
;
452 if (!r
->buf1
.memblock
|| r
->buf1_samples
< n_samples
) {
453 if (r
->buf1
.memblock
)
454 pa_memblock_unref(r
->buf1
.memblock
);
456 r
->buf1_samples
= n_samples
;
457 r
->buf1
.memblock
= pa_memblock_new(r
->mempool
, r
->buf1
.length
);
460 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
461 dst
= (uint8_t*) pa_memblock_acquire(r
->buf1
.memblock
);
463 r
->to_work_format_func(n_samples
, src
, dst
);
465 pa_memblock_release(input
->memblock
);
466 pa_memblock_release(r
->buf1
.memblock
);
471 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
472 unsigned in_n_samples
, out_n_samples
, n_frames
;
479 pa_assert(input
->memblock
);
481 /* Remap channels and place the result int buf2 */
483 if (!r
->map_required
|| !input
->length
)
486 in_n_samples
= input
->length
/ r
->w_sz
;
487 n_frames
= in_n_samples
/ r
->i_ss
.channels
;
488 out_n_samples
= n_frames
* r
->o_ss
.channels
;
491 r
->buf2
.length
= r
->w_sz
* out_n_samples
;
493 if (!r
->buf2
.memblock
|| r
->buf2_samples
< out_n_samples
) {
494 if (r
->buf2
.memblock
)
495 pa_memblock_unref(r
->buf2
.memblock
);
497 r
->buf2_samples
= out_n_samples
;
498 r
->buf2
.memblock
= pa_memblock_new(r
->mempool
, r
->buf2
.length
);
501 src
= ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
502 dst
= pa_memblock_acquire(r
->buf2
.memblock
);
504 memset(dst
, 0, r
->buf2
.length
);
506 o_skip
= r
->w_sz
* r
->o_ss
.channels
;
507 i_skip
= r
->w_sz
* r
->i_ss
.channels
;
509 switch (r
->work_format
) {
510 case PA_SAMPLE_FLOAT32NE
:
512 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
514 static const float one
= 1.0;
516 for (i
= 0; i
< PA_CHANNELS_MAX
&& r
->map_table
[oc
][i
] >= 0; i
++)
518 (float*) dst
+ oc
, o_skip
,
519 (float*) dst
+ oc
, o_skip
,
520 (float*) src
+ r
->map_table
[oc
][i
], i_skip
,
527 case PA_SAMPLE_S16NE
:
529 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
531 static const int16_t one
= 1;
533 for (i
= 0; i
< PA_CHANNELS_MAX
&& r
->map_table
[oc
][i
] >= 0; i
++)
535 (int16_t*) dst
+ oc
, o_skip
,
536 (int16_t*) dst
+ oc
, o_skip
,
537 (int16_t*) src
+ r
->map_table
[oc
][i
], i_skip
,
545 pa_assert_not_reached();
548 pa_memblock_release(input
->memblock
);
549 pa_memblock_release(r
->buf2
.memblock
);
551 r
->buf2
.length
= out_n_samples
* r
->w_sz
;
556 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
557 unsigned in_n_frames
, in_n_samples
;
558 unsigned out_n_frames
, out_n_samples
;
563 /* Resample the data and place the result in buf3 */
565 if (!r
->impl_resample
|| !input
->length
)
568 in_n_samples
= input
->length
/ r
->w_sz
;
569 in_n_frames
= in_n_samples
/ r
->o_ss
.channels
;
571 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+1024;
572 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
575 r
->buf3
.length
= r
->w_sz
* out_n_samples
;
577 if (!r
->buf3
.memblock
|| r
->buf3_samples
< out_n_samples
) {
578 if (r
->buf3
.memblock
)
579 pa_memblock_unref(r
->buf3
.memblock
);
581 r
->buf3_samples
= out_n_samples
;
582 r
->buf3
.memblock
= pa_memblock_new(r
->mempool
, r
->buf3
.length
);
585 r
->impl_resample(r
, input
, in_n_frames
, &r
->buf3
, &out_n_frames
);
586 r
->buf3
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
591 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
592 unsigned n_samples
, n_frames
;
598 /* Convert the data into the correct sample type and place the result in buf4 */
600 if (!r
->from_work_format_func
|| !input
->length
)
603 n_samples
= input
->length
/ r
->w_sz
;
604 n_frames
= n_samples
/ r
->o_ss
.channels
;
607 r
->buf4
.length
= r
->o_fz
* n_frames
;
609 if (!r
->buf4
.memblock
|| r
->buf4_samples
< n_samples
) {
610 if (r
->buf4
.memblock
)
611 pa_memblock_unref(r
->buf4
.memblock
);
613 r
->buf4_samples
= n_samples
;
614 r
->buf4
.memblock
= pa_memblock_new(r
->mempool
, r
->buf4
.length
);
617 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
618 dst
= pa_memblock_acquire(r
->buf4
.memblock
);
619 r
->from_work_format_func(n_samples
, src
, dst
);
620 pa_memblock_release(input
->memblock
);
621 pa_memblock_release(r
->buf4
.memblock
);
623 r
->buf4
.length
= r
->o_fz
* n_frames
;
628 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
634 pa_assert(in
->length
);
635 pa_assert(in
->memblock
);
636 pa_assert(in
->length
% r
->i_fz
== 0);
638 buf
= (pa_memchunk
*) in
;
639 buf
= convert_to_work_format(r
, buf
);
640 buf
= remap_channels(r
, buf
);
641 buf
= resample(r
, buf
);
644 buf
= convert_from_work_format(r
, buf
);
648 pa_memblock_ref(buf
->memblock
);
650 pa_memchunk_reset(buf
);
652 pa_memchunk_reset(out
);
655 /*** libsamplerate based implementation ***/
657 #ifdef HAVE_LIBSAMPLERATE
658 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
664 pa_assert(out_n_frames
);
666 memset(&data
, 0, sizeof(data
));
668 data
.data_in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
669 data
.input_frames
= in_n_frames
;
671 data
.data_out
= (float*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
672 data
.output_frames
= *out_n_frames
;
674 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
675 data
.end_of_input
= 0;
677 pa_assert_se(src_process(r
->src
.state
, &data
) == 0);
678 pa_assert((unsigned) data
.input_frames_used
== in_n_frames
);
680 pa_memblock_release(input
->memblock
);
681 pa_memblock_release(output
->memblock
);
683 *out_n_frames
= data
.output_frames_gen
;
686 static void libsamplerate_update_rates(pa_resampler
*r
) {
689 pa_assert_se(src_set_ratio(r
->src
.state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
692 static void libsamplerate_free(pa_resampler
*r
) {
696 src_delete(r
->src
.state
);
699 static int libsamplerate_init(pa_resampler
*r
) {
704 if (!(r
->src
.state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
707 r
->impl_free
= libsamplerate_free
;
708 r
->impl_update_rates
= libsamplerate_update_rates
;
709 r
->impl_resample
= libsamplerate_resample
;
715 /*** speex based implementation ***/
717 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
719 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
724 pa_assert(out_n_frames
);
726 in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
727 out
= (float*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
729 pa_assert_se(paspfl_resampler_process_interleaved_float(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
731 pa_memblock_release(input
->memblock
);
732 pa_memblock_release(output
->memblock
);
734 pa_assert(inf
== in_n_frames
);
735 *out_n_frames
= outf
;
738 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
740 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
745 pa_assert(out_n_frames
);
747 in
= (int16_t*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
748 out
= (int16_t*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
750 pa_assert_se(paspfx_resampler_process_interleaved_int(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
752 pa_memblock_release(input
->memblock
);
753 pa_memblock_release(output
->memblock
);
755 pa_assert(inf
== in_n_frames
);
756 *out_n_frames
= outf
;
759 static void speex_update_rates(pa_resampler
*r
) {
762 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
763 pa_assert_se(paspfx_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
765 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
766 pa_assert_se(paspfl_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
770 static void speex_free(pa_resampler
*r
) {
776 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
777 paspfx_resampler_destroy(r
->speex
.state
);
779 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
780 paspfl_resampler_destroy(r
->speex
.state
);
784 static int speex_init(pa_resampler
*r
) {
789 r
->impl_free
= speex_free
;
790 r
->impl_update_rates
= speex_update_rates
;
792 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
793 q
= r
->resample_method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
795 pa_log_info("Choosing speex quality setting %i.", q
);
797 if (!(r
->speex
.state
= paspfx_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
800 r
->impl_resample
= speex_resample_int
;
802 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
803 q
= r
->resample_method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
805 pa_log_info("Choosing speex quality setting %i.", q
);
807 if (!(r
->speex
.state
= paspfl_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
810 r
->impl_resample
= speex_resample_float
;
816 /* Trivial implementation */
818 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
826 pa_assert(out_n_frames
);
828 fz
= r
->w_sz
* r
->o_ss
.channels
;
830 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
831 dst
= (uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
;
833 for (o_index
= 0;; o_index
++, r
->trivial
.o_counter
++) {
836 j
= ((r
->trivial
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
);
837 j
= j
> r
->trivial
.i_counter
? j
- r
->trivial
.i_counter
: 0;
839 if (j
>= in_n_frames
)
842 pa_assert(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
844 oil_memcpy((uint8_t*) dst
+ fz
* o_index
,
845 (uint8_t*) src
+ fz
* j
, fz
);
848 pa_memblock_release(input
->memblock
);
849 pa_memblock_release(output
->memblock
);
851 *out_n_frames
= o_index
;
853 r
->trivial
.i_counter
+= in_n_frames
;
855 /* Normalize counters */
856 while (r
->trivial
.i_counter
>= r
->i_ss
.rate
) {
857 pa_assert(r
->trivial
.o_counter
>= r
->o_ss
.rate
);
859 r
->trivial
.i_counter
-= r
->i_ss
.rate
;
860 r
->trivial
.o_counter
-= r
->o_ss
.rate
;
864 static void trivial_update_rates(pa_resampler
*r
) {
867 r
->trivial
.i_counter
= 0;
868 r
->trivial
.o_counter
= 0;
871 static int trivial_init(pa_resampler
*r
) {
874 r
->trivial
.o_counter
= r
->trivial
.i_counter
= 0;
876 r
->impl_resample
= trivial_resample
;
877 r
->impl_update_rates
= trivial_update_rates
;
882 /*** ffmpeg based implementation ***/
884 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
885 unsigned used_frames
= 0, c
;
890 pa_assert(out_n_frames
);
892 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
895 int16_t *p
, *t
, *k
, *q
, *s
;
899 /* Allocate a new block */
900 b
= pa_memblock_new(r
->mempool
, r
->ffmpeg
.buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
901 p
= pa_memblock_acquire(b
);
903 /* Copy the remaining data into it */
904 l
= r
->ffmpeg
.buf
[c
].length
;
905 if (r
->ffmpeg
.buf
[c
].memblock
) {
906 t
= (int16_t*) ((uint8_t*) pa_memblock_acquire(r
->ffmpeg
.buf
[c
].memblock
) + r
->ffmpeg
.buf
[c
].index
);
908 pa_memblock_release(r
->ffmpeg
.buf
[c
].memblock
);
909 pa_memblock_unref(r
->ffmpeg
.buf
[c
].memblock
);
910 pa_memchunk_reset(&r
->ffmpeg
.buf
[c
]);
913 /* Now append the new data, splitting up channels */
914 t
= ((int16_t*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
)) + c
;
915 k
= (int16_t*) ((uint8_t*) p
+ l
);
916 for (u
= 0; u
< in_n_frames
; u
++) {
918 t
+= r
->o_ss
.channels
;
921 pa_memblock_release(input
->memblock
);
923 /* Calculate the resulting number of frames */
924 in
= in_n_frames
+ l
/ sizeof(int16_t);
926 /* Allocate buffer for the result */
927 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
928 q
= pa_memblock_acquire(w
);
931 used_frames
= av_resample(r
->ffmpeg
.state
,
935 c
>= (unsigned) r
->o_ss
.channels
-1);
937 pa_memblock_release(b
);
939 /* Now store the remaining samples away */
940 pa_assert(consumed_frames
<= (int) in
);
941 if (consumed_frames
< (int) in
) {
942 r
->ffmpeg
.buf
[c
].memblock
= b
;
943 r
->ffmpeg
.buf
[c
].index
= consumed_frames
* sizeof(int16_t);
944 r
->ffmpeg
.buf
[c
].length
= (in
- consumed_frames
) * sizeof(int16_t);
946 pa_memblock_unref(b
);
948 /* And place the results in the output buffer */
949 s
= (short*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
) + c
;
950 for (u
= 0; u
< used_frames
; u
++) {
953 s
+= r
->o_ss
.channels
;
955 pa_memblock_release(output
->memblock
);
956 pa_memblock_release(w
);
957 pa_memblock_unref(w
);
960 *out_n_frames
= used_frames
;
963 static void ffmpeg_free(pa_resampler
*r
) {
969 av_resample_close(r
->ffmpeg
.state
);
971 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
972 if (r
->ffmpeg
.buf
[c
].memblock
)
973 pa_memblock_unref(r
->ffmpeg
.buf
[c
].memblock
);
976 static int ffmpeg_init(pa_resampler
*r
) {
981 /* We could probably implement different quality levels by
982 * adjusting the filter parameters here. However, ffmpeg
983 * internally only uses these hardcoded values, so let's use them
984 * here for now as well until ffmpeg makes this configurable. */
986 if (!(r
->ffmpeg
.state
= av_resample_init(r
->o_ss
.rate
, r
->i_ss
.rate
, 16, 10, 0, 0.8)))
989 r
->impl_free
= ffmpeg_free
;
990 r
->impl_resample
= ffmpeg_resample
;
992 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
993 pa_memchunk_reset(&r
->ffmpeg
.buf
[c
]);