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 #include <samplerate.h>
32 #include <liboil/liboilfuncs.h>
33 #include <liboil/liboil.h>
35 #include <pulse/xmalloc.h>
36 #include <pulsecore/sconv.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
40 #include "speexwrap.h"
42 #include "ffmpeg/avcodec.h"
44 #include "resampler.h"
47 pa_resample_method_t resample_method
;
48 pa_sample_spec i_ss
, o_ss
;
49 pa_channel_map i_cm
, o_cm
;
50 size_t i_fz
, o_fz
, w_sz
;
53 pa_memchunk buf1
, buf2
, buf3
, buf4
;
54 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
56 pa_sample_format_t work_format
;
58 pa_convert_func_t to_work_format_func
;
59 pa_convert_func_t from_work_format_func
;
61 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
64 void (*impl_free
)(pa_resampler
*r
);
65 void (*impl_update_rates
)(pa_resampler
*r
);
66 void (*impl_resample
)(pa_resampler
*r
, const pa_memchunk
*in
, unsigned in_samples
, pa_memchunk
*out
, unsigned *out_samples
);
68 struct { /* data specific to the trivial resampler */
73 struct { /* data specific to libsamplerate */
77 struct { /* data specific to speex */
78 SpeexResamplerState
* state
;
81 struct { /* data specific to ffmpeg */
82 struct AVResampleContext
*state
;
83 unsigned initial_i_rate
, initial_o_rate
;
87 static int libsamplerate_init(pa_resampler
*r
);
88 static int trivial_init(pa_resampler
*r
);
89 static int speex_init(pa_resampler
*r
);
90 static int ffmpeg_init(pa_resampler
*r
);
92 static void calc_map_table(pa_resampler
*r
);
94 static int (* const init_table
[])(pa_resampler
*r
) = {
95 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
96 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
97 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
98 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
99 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
100 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
101 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
102 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
103 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
104 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
105 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
106 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
107 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
108 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
109 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
110 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
111 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
112 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
113 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
114 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
115 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
116 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
117 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
118 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
119 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
120 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
121 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
122 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
123 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
124 [PA_RESAMPLER_AUTO
] = NULL
,
127 static inline size_t sample_size(pa_sample_format_t f
) {
128 pa_sample_spec ss
= {
134 return pa_sample_size(&ss
);
137 pa_resampler
* pa_resampler_new(
139 const pa_sample_spec
*a
,
140 const pa_channel_map
*am
,
141 const pa_sample_spec
*b
,
142 const pa_channel_map
*bm
,
143 pa_resample_method_t resample_method
,
146 pa_resampler
*r
= NULL
;
151 pa_assert(pa_sample_spec_valid(a
));
152 pa_assert(pa_sample_spec_valid(b
));
153 pa_assert(resample_method
>= 0);
154 pa_assert(resample_method
< PA_RESAMPLER_MAX
);
158 if (resample_method
== PA_RESAMPLER_FFMPEG
&& variable_rate
) {
159 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'." );
160 resample_method
= PA_RESAMPLER_AUTO
;
163 if (resample_method
== PA_RESAMPLER_AUTO
) {
164 if (a
->format
== PA_SAMPLE_FLOAT32LE
|| a
->format
== PA_SAMPLE_FLOAT32BE
||
165 b
->format
== PA_SAMPLE_FLOAT32LE
|| b
->format
== PA_SAMPLE_FLOAT32BE
)
166 resample_method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 0;
168 resample_method
= PA_RESAMPLER_SPEEX_FIXED_BASE
+ 0;
171 r
= pa_xnew(pa_resampler
, 1);
173 r
->resample_method
= resample_method
;
176 r
->impl_update_rates
= NULL
;
177 r
->impl_resample
= NULL
;
179 /* Fill sample specs */
186 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
191 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
193 r
->i_fz
= pa_frame_size(a
);
194 r
->o_fz
= pa_frame_size(b
);
196 pa_memchunk_reset(&r
->buf1
);
197 pa_memchunk_reset(&r
->buf2
);
198 pa_memchunk_reset(&r
->buf3
);
199 pa_memchunk_reset(&r
->buf4
);
201 r
->buf1_samples
= r
->buf2_samples
= r
->buf3_samples
= r
->buf4_samples
= 0;
205 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(resample_method
));
207 if ((resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
208 (resample_method
== PA_RESAMPLER_FFMPEG
))
209 r
->work_format
= PA_SAMPLE_S16NE
;
210 else if (resample_method
== PA_RESAMPLER_TRIVIAL
) {
212 if (r
->map_required
|| a
->format
!= b
->format
) {
214 if (a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
)
215 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
217 r
->work_format
= PA_SAMPLE_S16NE
;
220 r
->work_format
= a
->format
;
223 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
225 r
->w_sz
= sample_size(r
->work_format
);
227 if (r
->i_ss
.format
== r
->work_format
)
228 r
->to_work_format_func
= NULL
;
229 else if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
230 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
233 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
234 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
238 if (r
->o_ss
.format
== r
->work_format
)
239 r
->from_work_format_func
= NULL
;
240 else if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
241 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
244 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
245 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
249 /* initialize implementation */
250 if (init_table
[resample_method
](r
) < 0)
262 void pa_resampler_free(pa_resampler
*r
) {
268 if (r
->buf1
.memblock
)
269 pa_memblock_unref(r
->buf1
.memblock
);
270 if (r
->buf2
.memblock
)
271 pa_memblock_unref(r
->buf2
.memblock
);
272 if (r
->buf3
.memblock
)
273 pa_memblock_unref(r
->buf3
.memblock
);
274 if (r
->buf4
.memblock
)
275 pa_memblock_unref(r
->buf4
.memblock
);
280 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
284 if (r
->i_ss
.rate
== rate
)
289 r
->impl_update_rates(r
);
292 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
296 if (r
->o_ss
.rate
== rate
)
301 r
->impl_update_rates(r
);
304 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
307 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
310 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
313 return r
->resample_method
;
316 static const char * const resample_methods
[] = {
317 "src-sinc-best-quality",
318 "src-sinc-medium-quality",
320 "src-zero-order-hold",
349 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
351 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
354 return resample_methods
[m
];
357 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
358 pa_resample_method_t m
;
362 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
363 if (!strcmp(string
, resample_methods
[m
]))
366 if (!strcmp(string
, "speex-fixed"))
367 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 0;
369 if (!strcmp(string
, "speex-float"))
370 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 0;
372 return PA_RESAMPLER_INVALID
;
375 static void calc_map_table(pa_resampler
*r
) {
380 if (!(r
->map_required
= (r
->i_ss
.channels
!= r
->o_ss
.channels
|| !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
))))
383 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
386 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
387 pa_channel_position_t a
, b
;
393 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
394 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
395 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
396 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
398 r
->map_table
[oc
][i
++] = ic
;
401 /* Add an end marker */
402 if (i
< PA_CHANNELS_MAX
)
403 r
->map_table
[oc
][i
] = -1;
407 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
413 pa_assert(input
->memblock
);
415 /* Convert the incoming sample into the work sample format and place them in buf1 */
417 if (!r
->to_work_format_func
|| !input
->length
)
420 n_samples
= (input
->length
/ r
->i_fz
) * r
->i_ss
.channels
;
423 r
->buf1
.length
= r
->w_sz
* n_samples
;
425 if (!r
->buf1
.memblock
|| r
->buf1_samples
< n_samples
) {
426 if (r
->buf1
.memblock
)
427 pa_memblock_unref(r
->buf1
.memblock
);
429 r
->buf1_samples
= n_samples
;
430 r
->buf1
.memblock
= pa_memblock_new(r
->mempool
, r
->buf1
.length
);
433 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
434 dst
= (uint8_t*) pa_memblock_acquire(r
->buf1
.memblock
);
436 r
->to_work_format_func(n_samples
, src
, dst
);
438 pa_memblock_release(input
->memblock
);
439 pa_memblock_release(r
->buf1
.memblock
);
444 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
445 unsigned in_n_samples
, out_n_samples
, n_frames
;
452 pa_assert(input
->memblock
);
454 /* Remap channels and place the result int buf2 */
456 if (!r
->map_required
|| !input
->length
)
459 in_n_samples
= input
->length
/ r
->w_sz
;
460 n_frames
= in_n_samples
/ r
->i_ss
.channels
;
461 out_n_samples
= n_frames
* r
->o_ss
.channels
;
464 r
->buf2
.length
= r
->w_sz
* out_n_samples
;
466 if (!r
->buf2
.memblock
|| r
->buf2_samples
< out_n_samples
) {
467 if (r
->buf2
.memblock
)
468 pa_memblock_unref(r
->buf2
.memblock
);
470 r
->buf2_samples
= out_n_samples
;
471 r
->buf2
.memblock
= pa_memblock_new(r
->mempool
, r
->buf2
.length
);
474 src
= ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
475 dst
= pa_memblock_acquire(r
->buf2
.memblock
);
477 memset(dst
, 0, r
->buf2
.length
);
479 o_skip
= r
->w_sz
* r
->o_ss
.channels
;
480 i_skip
= r
->w_sz
* r
->i_ss
.channels
;
482 switch (r
->work_format
) {
483 case PA_SAMPLE_FLOAT32NE
:
485 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
487 static const float one
= 1.0;
489 for (i
= 0; i
< PA_CHANNELS_MAX
&& r
->map_table
[oc
][i
] >= 0; i
++)
491 (float*) dst
+ oc
, o_skip
,
492 (float*) dst
+ oc
, o_skip
,
493 (float*) src
+ r
->map_table
[oc
][i
], i_skip
,
500 case PA_SAMPLE_S16NE
:
502 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
504 static const int16_t one
= 1;
506 for (i
= 0; i
< PA_CHANNELS_MAX
&& r
->map_table
[oc
][i
] >= 0; i
++)
508 (int16_t*) dst
+ oc
, o_skip
,
509 (int16_t*) dst
+ oc
, o_skip
,
510 (int16_t*) src
+ r
->map_table
[oc
][i
], i_skip
,
518 pa_assert_not_reached();
521 pa_memblock_release(input
->memblock
);
522 pa_memblock_release(r
->buf2
.memblock
);
524 r
->buf2
.length
= out_n_samples
* r
->w_sz
;
529 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
530 unsigned in_n_frames
, in_n_samples
;
531 unsigned out_n_frames
, out_n_samples
;
536 /* Resample the data and place the result in buf3 */
538 if (!r
->impl_resample
|| !input
->length
)
541 in_n_samples
= input
->length
/ r
->w_sz
;
542 in_n_frames
= in_n_samples
/ r
->o_ss
.channels
;
544 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+1024;
545 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
548 r
->buf3
.length
= r
->w_sz
* out_n_samples
;
550 if (!r
->buf3
.memblock
|| r
->buf3_samples
< out_n_samples
) {
551 if (r
->buf3
.memblock
)
552 pa_memblock_unref(r
->buf3
.memblock
);
554 r
->buf3_samples
= out_n_samples
;
555 r
->buf3
.memblock
= pa_memblock_new(r
->mempool
, r
->buf3
.length
);
558 r
->impl_resample(r
, input
, in_n_frames
, &r
->buf3
, &out_n_frames
);
559 r
->buf3
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
564 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
565 unsigned n_samples
, n_frames
;
571 /* Convert the data into the correct sample type and place the result in buf4 */
573 if (!r
->from_work_format_func
|| !input
->length
)
576 n_samples
= input
->length
/ r
->w_sz
;
577 n_frames
= n_samples
/ r
->o_ss
.channels
;
580 r
->buf4
.length
= r
->o_fz
* n_frames
;
582 if (!r
->buf4
.memblock
|| r
->buf4_samples
< n_samples
) {
583 if (r
->buf4
.memblock
)
584 pa_memblock_unref(r
->buf4
.memblock
);
586 r
->buf4_samples
= n_samples
;
587 r
->buf4
.memblock
= pa_memblock_new(r
->mempool
, r
->buf4
.length
);
590 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
591 dst
= pa_memblock_acquire(r
->buf4
.memblock
);
592 r
->from_work_format_func(n_samples
, src
, dst
);
593 pa_memblock_release(input
->memblock
);
594 pa_memblock_release(r
->buf4
.memblock
);
596 r
->buf4
.length
= r
->o_fz
* n_frames
;
601 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
607 pa_assert(in
->length
);
608 pa_assert(in
->memblock
);
609 pa_assert(in
->length
% r
->i_fz
== 0);
611 buf
= (pa_memchunk
*) in
;
612 buf
= convert_to_work_format(r
, buf
);
613 buf
= remap_channels(r
, buf
);
614 buf
= resample(r
, buf
);
617 buf
= convert_from_work_format(r
, buf
);
621 pa_memblock_ref(buf
->memblock
);
623 pa_memchunk_reset(buf
);
625 pa_memchunk_reset(out
);
628 /*** libsamplerate based implementation ***/
630 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
636 pa_assert(out_n_frames
);
638 memset(&data
, 0, sizeof(data
));
640 data
.data_in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
641 data
.input_frames
= in_n_frames
;
643 data
.data_out
= (float*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
644 data
.output_frames
= *out_n_frames
;
646 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
647 data
.end_of_input
= 0;
649 pa_assert_se(src_process(r
->src
.state
, &data
) == 0);
650 pa_assert((unsigned) data
.input_frames_used
== in_n_frames
);
652 pa_memblock_release(input
->memblock
);
653 pa_memblock_release(output
->memblock
);
655 *out_n_frames
= data
.output_frames_gen
;
658 static void libsamplerate_update_rates(pa_resampler
*r
) {
661 pa_assert_se(src_set_ratio(r
->src
.state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
664 static void libsamplerate_free(pa_resampler
*r
) {
668 src_delete(r
->src
.state
);
671 static int libsamplerate_init(pa_resampler
*r
) {
676 if (!(r
->src
.state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
679 r
->impl_free
= libsamplerate_free
;
680 r
->impl_update_rates
= libsamplerate_update_rates
;
681 r
->impl_resample
= libsamplerate_resample
;
686 /*** speex based implementation ***/
688 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
690 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
695 pa_assert(out_n_frames
);
697 in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
698 out
= (float*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
700 pa_assert_se(paspfl_resampler_process_interleaved_float(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
702 pa_memblock_release(input
->memblock
);
703 pa_memblock_release(output
->memblock
);
705 pa_assert(inf
== in_n_frames
);
706 *out_n_frames
= outf
;
709 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
711 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
716 pa_assert(out_n_frames
);
718 in
= (int16_t*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
719 out
= (int16_t*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
721 pa_assert_se(paspfx_resampler_process_interleaved_int(r
->speex
.state
, in
, &inf
, out
, &outf
) == 0);
723 pa_memblock_release(input
->memblock
);
724 pa_memblock_release(output
->memblock
);
726 pa_assert(inf
== in_n_frames
);
727 *out_n_frames
= outf
;
730 static void speex_update_rates(pa_resampler
*r
) {
733 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
734 pa_assert_se(paspfx_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
736 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
737 pa_assert_se(paspfl_resampler_set_rate(r
->speex
.state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
741 static void speex_free(pa_resampler
*r
) {
744 if (r
->speex
.state
) {
745 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
746 paspfx_resampler_destroy(r
->speex
.state
);
748 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
749 paspfl_resampler_destroy(r
->speex
.state
);
754 static int speex_init(pa_resampler
*r
) {
759 r
->impl_free
= speex_free
;
760 r
->impl_update_rates
= speex_update_rates
;
762 if (r
->resample_method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
763 q
= r
->resample_method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
764 r
->impl_resample
= speex_resample_int
;
766 if (!(r
->speex
.state
= paspfx_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
770 pa_assert(r
->resample_method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->resample_method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
771 q
= r
->resample_method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
772 r
->impl_resample
= speex_resample_float
;
774 if (!(r
->speex
.state
= paspfl_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
782 /* Trivial implementation */
784 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
792 pa_assert(out_n_frames
);
794 fz
= r
->w_sz
* r
->o_ss
.channels
;
796 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
797 dst
= (uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
;
799 for (o_index
= 0;; o_index
++, r
->trivial
.o_counter
++) {
802 j
= ((r
->trivial
.o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
);
803 j
= j
> r
->trivial
.i_counter
? j
- r
->trivial
.i_counter
: 0;
805 if (j
>= in_n_frames
)
808 pa_assert(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
810 oil_memcpy((uint8_t*) dst
+ fz
* o_index
,
811 (uint8_t*) src
+ fz
* j
, fz
);
814 pa_memblock_release(input
->memblock
);
815 pa_memblock_release(output
->memblock
);
817 *out_n_frames
= o_index
;
819 r
->trivial
.i_counter
+= in_n_frames
;
821 /* Normalize counters */
822 while (r
->trivial
.i_counter
>= r
->i_ss
.rate
) {
823 pa_assert(r
->trivial
.o_counter
>= r
->o_ss
.rate
);
825 r
->trivial
.i_counter
-= r
->i_ss
.rate
;
826 r
->trivial
.o_counter
-= r
->o_ss
.rate
;
830 static void trivial_update_rates(pa_resampler
*r
) {
833 r
->trivial
.i_counter
= 0;
834 r
->trivial
.o_counter
= 0;
837 static int trivial_init(pa_resampler
*r
) {
840 r
->trivial
.o_counter
= r
->trivial
.i_counter
= 0;
842 r
->impl_resample
= trivial_resample
;
843 r
->impl_update_rates
= trivial_update_rates
;
848 /*** ffmpeg based implementation ***/
850 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
858 pa_assert(out_n_frames
);
860 src
= (short*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
861 dst
= (short*) ((uint8_t*) pa_memblock_acquire(output
->memblock
) + output
->index
);
863 for (c
= 0; c
< r
->o_ss
.channels
; c
++)
864 *out_n_frames
= av_resample(r
->ffmpeg
.state
,
868 in_n_frames
, *out_n_frames
,
869 c
>= r
->o_ss
.channels
-1);
871 pa_assert(*out_n_frames
> 0);
872 pa_assert(consumed
== in_n_frames
);
874 pa_memblock_release(input
->memblock
);
875 pa_memblock_release(output
->memblock
);
878 static void ffmpeg_free(pa_resampler
*r
) {
882 av_resample_close(r
->ffmpeg
.state
);
885 static int ffmpeg_init(pa_resampler
*r
) {
888 /* We could probably implement different quality levels by
889 * adjusting the filter parameters here. However, ffmpeg
890 * internally only uses these hardcoded values, so let's use them
891 * here for now as well until ffmpeg makes this configurable. */
893 if (!(r
->ffmpeg
.state
= av_resample_init(r
->o_ss
.rate
, r
->i_ss
.rate
, 16, 10, 0, 0.8)))
896 r
->impl_free
= ffmpeg_free
;
897 r
->impl_resample
= ffmpeg_resample
;