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
);
95 static void calc_map_table(pa_resampler
*r
);
97 static int (* const init_table
[])(pa_resampler
*r
) = {
98 #ifdef HAVE_LIBSAMPLERATE
99 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
100 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
101 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
102 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
103 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
105 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
106 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
107 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
108 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
109 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
111 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
112 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
113 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
114 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
115 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
116 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
117 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
118 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
119 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
120 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
121 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
122 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
123 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
124 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
125 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
126 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
127 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
128 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
129 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
130 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
131 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
132 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
133 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
134 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
135 [PA_RESAMPLER_AUTO
] = NULL
,
138 static inline size_t sample_size(pa_sample_format_t f
) {
139 pa_sample_spec ss
= {
145 return pa_sample_size(&ss
);
148 pa_resampler
* pa_resampler_new(
150 const pa_sample_spec
*a
,
151 const pa_channel_map
*am
,
152 const pa_sample_spec
*b
,
153 const pa_channel_map
*bm
,
154 pa_resample_method_t resample_method
,
157 pa_resampler
*r
= NULL
;
162 pa_assert(pa_sample_spec_valid(a
));
163 pa_assert(pa_sample_spec_valid(b
));
164 pa_assert(resample_method
>= 0);
165 pa_assert(resample_method
< PA_RESAMPLER_MAX
);
169 if (resample_method
== PA_RESAMPLER_FFMPEG
&& variable_rate
) {
170 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'." );
171 resample_method
= PA_RESAMPLER_AUTO
;
174 if (resample_method
== PA_RESAMPLER_AUTO
)
175 resample_method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 0;
177 r
= pa_xnew(pa_resampler
, 1);
179 r
->resample_method
= resample_method
;
182 r
->impl_update_rates
= NULL
;
183 r
->impl_resample
= NULL
;
185 /* Fill sample specs */
192 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
197 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
199 r
->i_fz
= pa_frame_size(a
);
200 r
->o_fz
= pa_frame_size(b
);
202 pa_memchunk_reset(&r
->buf1
);
203 pa_memchunk_reset(&r
->buf2
);
204 pa_memchunk_reset(&r
->buf3
);
205 pa_memchunk_reset(&r
->buf4
);
207 r
->buf1_samples
= r
->buf2_samples
= r
->buf3_samples
= r
->buf4_samples
= 0;
211 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(resample_method
));
213 if ((resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
214 (resample_method
== PA_RESAMPLER_FFMPEG
))
215 r
->work_format
= PA_SAMPLE_S16NE
;
216 else if (resample_method
== PA_RESAMPLER_TRIVIAL
) {
218 if (r
->map_required
|| a
->format
!= b
->format
) {
220 if (a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
)
221 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
223 r
->work_format
= PA_SAMPLE_S16NE
;
226 r
->work_format
= a
->format
;
229 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
231 r
->w_sz
= sample_size(r
->work_format
);
233 if (r
->i_ss
.format
== r
->work_format
)
234 r
->to_work_format_func
= NULL
;
235 else if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
236 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
239 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
240 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
244 if (r
->o_ss
.format
== r
->work_format
)
245 r
->from_work_format_func
= NULL
;
246 else if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
247 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
250 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
251 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
255 /* initialize implementation */
256 if (init_table
[resample_method
](r
) < 0)
268 void pa_resampler_free(pa_resampler
*r
) {
274 if (r
->buf1
.memblock
)
275 pa_memblock_unref(r
->buf1
.memblock
);
276 if (r
->buf2
.memblock
)
277 pa_memblock_unref(r
->buf2
.memblock
);
278 if (r
->buf3
.memblock
)
279 pa_memblock_unref(r
->buf3
.memblock
);
280 if (r
->buf4
.memblock
)
281 pa_memblock_unref(r
->buf4
.memblock
);
286 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
290 if (r
->i_ss
.rate
== rate
)
295 r
->impl_update_rates(r
);
298 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
302 if (r
->o_ss
.rate
== rate
)
307 r
->impl_update_rates(r
);
310 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
313 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
316 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
319 return r
->resample_method
;
322 static const char * const resample_methods
[] = {
323 "src-sinc-best-quality",
324 "src-sinc-medium-quality",
326 "src-zero-order-hold",
355 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
357 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
360 return resample_methods
[m
];
363 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
364 pa_resample_method_t m
;
368 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
369 if (!strcmp(string
, resample_methods
[m
]))
372 if (!strcmp(string
, "speex-fixed"))
373 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 0;
375 if (!strcmp(string
, "speex-float"))
376 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 0;
378 return PA_RESAMPLER_INVALID
;
381 static void calc_map_table(pa_resampler
*r
) {
386 if (!(r
->map_required
= (r
->i_ss
.channels
!= r
->o_ss
.channels
|| !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
))))
389 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
392 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
393 pa_channel_position_t a
, b
;
399 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
400 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
401 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
402 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
404 r
->map_table
[oc
][i
++] = ic
;
407 /* Add an end marker */
408 if (i
< PA_CHANNELS_MAX
)
409 r
->map_table
[oc
][i
] = -1;
413 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
419 pa_assert(input
->memblock
);
421 /* Convert the incoming sample into the work sample format and place them in buf1 */
423 if (!r
->to_work_format_func
|| !input
->length
)
426 n_samples
= (input
->length
/ r
->i_fz
) * r
->i_ss
.channels
;
429 r
->buf1
.length
= r
->w_sz
* n_samples
;
431 if (!r
->buf1
.memblock
|| r
->buf1_samples
< n_samples
) {
432 if (r
->buf1
.memblock
)
433 pa_memblock_unref(r
->buf1
.memblock
);
435 r
->buf1_samples
= n_samples
;
436 r
->buf1
.memblock
= pa_memblock_new(r
->mempool
, r
->buf1
.length
);
439 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
440 dst
= (uint8_t*) pa_memblock_acquire(r
->buf1
.memblock
);
442 r
->to_work_format_func(n_samples
, src
, dst
);
444 pa_memblock_release(input
->memblock
);
445 pa_memblock_release(r
->buf1
.memblock
);
450 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
451 unsigned in_n_samples
, out_n_samples
, n_frames
;
458 pa_assert(input
->memblock
);
460 /* Remap channels and place the result int buf2 */
462 if (!r
->map_required
|| !input
->length
)
465 in_n_samples
= input
->length
/ r
->w_sz
;
466 n_frames
= in_n_samples
/ r
->i_ss
.channels
;
467 out_n_samples
= n_frames
* r
->o_ss
.channels
;
470 r
->buf2
.length
= r
->w_sz
* out_n_samples
;
472 if (!r
->buf2
.memblock
|| r
->buf2_samples
< out_n_samples
) {
473 if (r
->buf2
.memblock
)
474 pa_memblock_unref(r
->buf2
.memblock
);
476 r
->buf2_samples
= out_n_samples
;
477 r
->buf2
.memblock
= pa_memblock_new(r
->mempool
, r
->buf2
.length
);
480 src
= ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
481 dst
= pa_memblock_acquire(r
->buf2
.memblock
);
483 memset(dst
, 0, r
->buf2
.length
);
485 o_skip
= r
->w_sz
* r
->o_ss
.channels
;
486 i_skip
= r
->w_sz
* r
->i_ss
.channels
;
488 switch (r
->work_format
) {
489 case PA_SAMPLE_FLOAT32NE
:
491 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
493 static const float one
= 1.0;
495 for (i
= 0; i
< PA_CHANNELS_MAX
&& r
->map_table
[oc
][i
] >= 0; i
++)
497 (float*) dst
+ oc
, o_skip
,
498 (float*) dst
+ oc
, o_skip
,
499 (float*) src
+ r
->map_table
[oc
][i
], i_skip
,
506 case PA_SAMPLE_S16NE
:
508 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
510 static const int16_t one
= 1;
512 for (i
= 0; i
< PA_CHANNELS_MAX
&& r
->map_table
[oc
][i
] >= 0; i
++)
514 (int16_t*) dst
+ oc
, o_skip
,
515 (int16_t*) dst
+ oc
, o_skip
,
516 (int16_t*) src
+ r
->map_table
[oc
][i
], i_skip
,
524 pa_assert_not_reached();
527 pa_memblock_release(input
->memblock
);
528 pa_memblock_release(r
->buf2
.memblock
);
530 r
->buf2
.length
= out_n_samples
* r
->w_sz
;
535 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
536 unsigned in_n_frames
, in_n_samples
;
537 unsigned out_n_frames
, out_n_samples
;
542 /* Resample the data and place the result in buf3 */
544 if (!r
->impl_resample
|| !input
->length
)
547 in_n_samples
= input
->length
/ r
->w_sz
;
548 in_n_frames
= in_n_samples
/ r
->o_ss
.channels
;
550 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+1024;
551 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
554 r
->buf3
.length
= r
->w_sz
* out_n_samples
;
556 if (!r
->buf3
.memblock
|| r
->buf3_samples
< out_n_samples
) {
557 if (r
->buf3
.memblock
)
558 pa_memblock_unref(r
->buf3
.memblock
);
560 r
->buf3_samples
= out_n_samples
;
561 r
->buf3
.memblock
= pa_memblock_new(r
->mempool
, r
->buf3
.length
);
564 r
->impl_resample(r
, input
, in_n_frames
, &r
->buf3
, &out_n_frames
);
565 r
->buf3
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
570 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
571 unsigned n_samples
, n_frames
;
577 /* Convert the data into the correct sample type and place the result in buf4 */
579 if (!r
->from_work_format_func
|| !input
->length
)
582 n_samples
= input
->length
/ r
->w_sz
;
583 n_frames
= n_samples
/ r
->o_ss
.channels
;
586 r
->buf4
.length
= r
->o_fz
* n_frames
;
588 if (!r
->buf4
.memblock
|| r
->buf4_samples
< n_samples
) {
589 if (r
->buf4
.memblock
)
590 pa_memblock_unref(r
->buf4
.memblock
);
592 r
->buf4_samples
= n_samples
;
593 r
->buf4
.memblock
= pa_memblock_new(r
->mempool
, r
->buf4
.length
);
596 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
597 dst
= pa_memblock_acquire(r
->buf4
.memblock
);
598 r
->from_work_format_func(n_samples
, src
, dst
);
599 pa_memblock_release(input
->memblock
);
600 pa_memblock_release(r
->buf4
.memblock
);
602 r
->buf4
.length
= r
->o_fz
* n_frames
;
607 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
613 pa_assert(in
->length
);
614 pa_assert(in
->memblock
);
615 pa_assert(in
->length
% r
->i_fz
== 0);
617 buf
= (pa_memchunk
*) in
;
618 buf
= convert_to_work_format(r
, buf
);
619 buf
= remap_channels(r
, buf
);
620 buf
= resample(r
, buf
);
623 buf
= convert_from_work_format(r
, buf
);
627 pa_memblock_ref(buf
->memblock
);
629 pa_memchunk_reset(buf
);
631 pa_memchunk_reset(out
);
634 /*** libsamplerate based implementation ***/
636 #ifdef HAVE_LIBSAMPLERATE
637 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
643 pa_assert(out_n_frames
);
645 memset(&data
, 0, sizeof(data
));
647 data
.data_in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
648 data
.input_frames
= in_n_frames
;
650 data
.data_out
= (float*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
651 data
.output_frames
= *out_n_frames
;
653 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
654 data
.end_of_input
= 0;
656 pa_assert_se(src_process(r
->src
.state
, &data
) == 0);
657 pa_assert((unsigned) data
.input_frames_used
== in_n_frames
);
659 pa_memblock_release(input
->memblock
);
660 pa_memblock_release(output
->memblock
);
662 *out_n_frames
= data
.output_frames_gen
;
665 static void libsamplerate_update_rates(pa_resampler
*r
) {
668 pa_assert_se(src_set_ratio(r
->src
.state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
671 static void libsamplerate_free(pa_resampler
*r
) {
675 src_delete(r
->src
.state
);
678 static int libsamplerate_init(pa_resampler
*r
) {
683 if (!(r
->src
.state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
686 r
->impl_free
= libsamplerate_free
;
687 r
->impl_update_rates
= libsamplerate_update_rates
;
688 r
->impl_resample
= libsamplerate_resample
;
694 /*** speex based implementation ***/
696 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
698 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
703 pa_assert(out_n_frames
);
705 in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
706 out
= (float*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
708 pa_assert_se(paspfl_resampler_process_interleaved_float(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
710 pa_memblock_release(input
->memblock
);
711 pa_memblock_release(output
->memblock
);
713 pa_assert(inf
== in_n_frames
);
714 *out_n_frames
= outf
;
717 static void speex_resample_int(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
= (int16_t*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
727 out
= (int16_t*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
729 pa_assert_se(paspfx_resampler_process_interleaved_int(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_update_rates(pa_resampler
*r
) {
741 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
742 pa_assert_se(paspfx_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
744 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
745 pa_assert_se(paspfl_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
749 static void speex_free(pa_resampler
*r
) {
755 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
756 paspfx_resampler_destroy(r
->speex
.state
);
758 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
759 paspfl_resampler_destroy(r
->speex
.state
);
763 static int speex_init(pa_resampler
*r
) {
768 r
->impl_free
= speex_free
;
769 r
->impl_update_rates
= speex_update_rates
;
771 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
772 q
= r
->resample_method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
774 pa_log_info("Choosing speex quality setting %i.", q
);
776 if (!(r
->speex
.state
= paspfx_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
779 r
->impl_resample
= speex_resample_int
;
781 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
782 q
= r
->resample_method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
784 pa_log_info("Choosing speex quality setting %i.", q
);
786 if (!(r
->speex
.state
= paspfl_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
789 r
->impl_resample
= speex_resample_float
;
795 /* Trivial implementation */
797 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
805 pa_assert(out_n_frames
);
807 fz
= r
->w_sz
* r
->o_ss
.channels
;
809 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
810 dst
= (uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
;
812 for (o_index
= 0;; o_index
++, r
->trivial
.o_counter
++) {
815 j
= ((r
->trivial
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
);
816 j
= j
> r
->trivial
.i_counter
? j
- r
->trivial
.i_counter
: 0;
818 if (j
>= in_n_frames
)
821 pa_assert(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
823 oil_memcpy((uint8_t*) dst
+ fz
* o_index
,
824 (uint8_t*) src
+ fz
* j
, fz
);
827 pa_memblock_release(input
->memblock
);
828 pa_memblock_release(output
->memblock
);
830 *out_n_frames
= o_index
;
832 r
->trivial
.i_counter
+= in_n_frames
;
834 /* Normalize counters */
835 while (r
->trivial
.i_counter
>= r
->i_ss
.rate
) {
836 pa_assert(r
->trivial
.o_counter
>= r
->o_ss
.rate
);
838 r
->trivial
.i_counter
-= r
->i_ss
.rate
;
839 r
->trivial
.o_counter
-= r
->o_ss
.rate
;
843 static void trivial_update_rates(pa_resampler
*r
) {
846 r
->trivial
.i_counter
= 0;
847 r
->trivial
.o_counter
= 0;
850 static int trivial_init(pa_resampler
*r
) {
853 r
->trivial
.o_counter
= r
->trivial
.i_counter
= 0;
855 r
->impl_resample
= trivial_resample
;
856 r
->impl_update_rates
= trivial_update_rates
;
861 /*** ffmpeg based implementation ***/
863 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
864 unsigned used_frames
= 0, c
;
869 pa_assert(out_n_frames
);
871 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
874 int16_t *p
, *t
, *k
, *q
, *s
;
878 /* Allocate a new block */
879 b
= pa_memblock_new(r
->mempool
, r
->ffmpeg
.buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
880 p
= pa_memblock_acquire(b
);
882 /* Copy the remaining data into it */
883 l
= r
->ffmpeg
.buf
[c
].length
;
884 if (r
->ffmpeg
.buf
[c
].memblock
) {
885 t
= (int16_t*) ((uint8_t*) pa_memblock_acquire(r
->ffmpeg
.buf
[c
].memblock
) + r
->ffmpeg
.buf
[c
].index
);
887 pa_memblock_release(r
->ffmpeg
.buf
[c
].memblock
);
888 pa_memblock_unref(r
->ffmpeg
.buf
[c
].memblock
);
889 pa_memchunk_reset(&r
->ffmpeg
.buf
[c
]);
892 /* Now append the new data, splitting up channels */
893 t
= ((int16_t*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
)) + c
;
894 k
= (int16_t*) ((uint8_t*) p
+ l
);
895 for (u
= 0; u
< in_n_frames
; u
++) {
897 t
+= r
->o_ss
.channels
;
900 pa_memblock_release(input
->memblock
);
902 /* Calculate the resulting number of frames */
903 in
= in_n_frames
+ l
/ sizeof(int16_t);
905 /* Allocate buffer for the result */
906 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
907 q
= pa_memblock_acquire(w
);
910 used_frames
= av_resample(r
->ffmpeg
.state
,
914 c
>= (unsigned) r
->o_ss
.channels
-1);
916 pa_memblock_release(b
);
918 /* Now store the remaining samples away */
919 pa_assert(consumed_frames
<= (int) in
);
920 if (consumed_frames
< (int) in
) {
921 r
->ffmpeg
.buf
[c
].memblock
= b
;
922 r
->ffmpeg
.buf
[c
].index
= consumed_frames
* sizeof(int16_t);
923 r
->ffmpeg
.buf
[c
].length
= (in
- consumed_frames
) * sizeof(int16_t);
925 pa_memblock_unref(b
);
927 /* And place the results in the output buffer */
928 s
= (short*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
) + c
;
929 for (u
= 0; u
< used_frames
; u
++) {
932 s
+= r
->o_ss
.channels
;
934 pa_memblock_release(output
->memblock
);
935 pa_memblock_release(w
);
936 pa_memblock_unref(w
);
939 *out_n_frames
= used_frames
;
942 static void ffmpeg_free(pa_resampler
*r
) {
948 av_resample_close(r
->ffmpeg
.state
);
950 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
951 if (r
->ffmpeg
.buf
[c
].memblock
)
952 pa_memblock_unref(r
->ffmpeg
.buf
[c
].memblock
);
955 static int ffmpeg_init(pa_resampler
*r
) {
960 /* We could probably implement different quality levels by
961 * adjusting the filter parameters here. However, ffmpeg
962 * internally only uses these hardcoded values, so let's use them
963 * here for now as well until ffmpeg makes this configurable. */
965 if (!(r
->ffmpeg
.state
= av_resample_init(r
->o_ss
.rate
, r
->i_ss
.rate
, 16, 10, 0, 0.8)))
968 r
->impl_free
= ffmpeg_free
;
969 r
->impl_resample
= ffmpeg_resample
;
971 for (c
= 0; c
< PA_ELEMENTSOF(r
->ffmpeg
.buf
); c
++)
972 pa_memchunk_reset(&r
->ffmpeg
.buf
[c
]);