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>
31 #include <liboil/liboilfuncs.h>
32 #include <liboil/liboil.h>
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/sconv.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
39 #include "resampler.h"
42 pa_resample_method_t resample_method
;
43 pa_sample_spec i_ss
, o_ss
;
44 pa_channel_map i_cm
, o_cm
;
48 void (*impl_free
)(pa_resampler
*r
);
49 void (*impl_update_input_rate
)(pa_resampler
*r
, uint32_t rate
);
50 void (*impl_update_output_rate
)(pa_resampler
*r
, uint32_t rate
);
51 void (*impl_run
)(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
);
55 struct impl_libsamplerate
{
56 pa_memchunk buf1
, buf2
, buf3
, buf4
;
57 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
59 pa_convert_to_float32ne_func_t to_float32ne_func
;
60 pa_convert_from_float32ne_func_t from_float32ne_func
;
63 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
72 static int libsamplerate_init(pa_resampler
*r
);
73 static int trivial_init(pa_resampler
*r
);
75 pa_resampler
* pa_resampler_new(
77 const pa_sample_spec
*a
,
78 const pa_channel_map
*am
,
79 const pa_sample_spec
*b
,
80 const pa_channel_map
*bm
,
81 pa_resample_method_t resample_method
) {
83 pa_resampler
*r
= NULL
;
88 pa_assert(pa_sample_spec_valid(a
));
89 pa_assert(pa_sample_spec_valid(b
));
90 pa_assert(resample_method
!= PA_RESAMPLER_INVALID
);
92 r
= pa_xnew(pa_resampler
, 1);
95 r
->resample_method
= resample_method
;
98 r
->impl_update_input_rate
= NULL
;
101 /* Fill sample specs */
108 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
113 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
115 r
->i_fz
= pa_frame_size(a
);
116 r
->o_fz
= pa_frame_size(b
);
118 /* Choose implementation */
119 if (a
->channels
!= b
->channels
||
120 a
->format
!= b
->format
||
121 !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) ||
122 resample_method
!= PA_RESAMPLER_TRIVIAL
) {
124 /* Use the libsamplerate based resampler for the complicated cases */
125 if (resample_method
== PA_RESAMPLER_TRIVIAL
)
126 r
->resample_method
= PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
;
128 if (libsamplerate_init(r
) < 0)
132 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
133 if (trivial_init(r
) < 0)
146 void pa_resampler_free(pa_resampler
*r
) {
155 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
159 if (r
->i_ss
.rate
== rate
)
164 if (r
->impl_update_input_rate
)
165 r
->impl_update_input_rate(r
, rate
);
168 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
172 if (r
->o_ss
.rate
== rate
)
177 if (r
->impl_update_output_rate
)
178 r
->impl_update_output_rate(r
, rate
);
181 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
182 pa_assert(r
&& in
&& out
&& r
->impl_run
);
184 r
->impl_run(r
, in
, out
);
187 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
190 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
193 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
195 return r
->resample_method
;
198 static const char * const resample_methods
[] = {
199 "src-sinc-best-quality",
200 "src-sinc-medium-quality",
202 "src-zero-order-hold",
207 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
209 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
212 return resample_methods
[m
];
215 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
216 pa_resample_method_t m
;
220 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
221 if (!strcmp(string
, resample_methods
[m
]))
224 return PA_RESAMPLER_INVALID
;
228 /*** libsamplerate based implementation ***/
230 static void libsamplerate_free(pa_resampler
*r
) {
231 struct impl_libsamplerate
*u
;
234 pa_assert(r
->impl_data
);
239 src_delete(u
->src_state
);
241 if (u
->buf1
.memblock
)
242 pa_memblock_unref(u
->buf1
.memblock
);
243 if (u
->buf2
.memblock
)
244 pa_memblock_unref(u
->buf2
.memblock
);
245 if (u
->buf3
.memblock
)
246 pa_memblock_unref(u
->buf3
.memblock
);
247 if (u
->buf4
.memblock
)
248 pa_memblock_unref(u
->buf4
.memblock
);
252 static void calc_map_table(pa_resampler
*r
) {
253 struct impl_libsamplerate
*u
;
257 pa_assert(r
->impl_data
);
261 if (!(u
->map_required
= (r
->i_ss
.channels
!= r
->o_ss
.channels
|| !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
))))
264 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
267 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
268 pa_channel_position_t a
, b
;
274 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
275 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
276 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
277 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
279 u
->map_table
[oc
][i
++] = ic
;
282 /* Add an end marker */
283 if (i
< PA_CHANNELS_MAX
)
284 u
->map_table
[oc
][i
] = -1;
288 static pa_memchunk
* convert_to_float(pa_resampler
*r
, pa_memchunk
*input
) {
289 struct impl_libsamplerate
*u
;
295 pa_assert(input
->memblock
);
297 pa_assert(r
->impl_data
);
300 /* Convert the incoming sample into floats and place them in buf1 */
302 if (!u
->to_float32ne_func
|| !input
->length
)
305 n_samples
= (input
->length
/ r
->i_fz
) * r
->i_ss
.channels
;
307 if (!u
->buf1
.memblock
|| u
->buf1_samples
< n_samples
) {
308 if (u
->buf1
.memblock
)
309 pa_memblock_unref(u
->buf1
.memblock
);
311 u
->buf1_samples
= n_samples
;
312 u
->buf1
.memblock
= pa_memblock_new(r
->mempool
, u
->buf1
.length
= sizeof(float) * n_samples
);
316 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
317 dst
= (uint8_t*) pa_memblock_acquire(u
->buf1
.memblock
);
319 u
->to_float32ne_func(n_samples
, src
, dst
);
321 pa_memblock_release(input
->memblock
);
322 pa_memblock_release(u
->buf1
.memblock
);
324 u
->buf1
.length
= sizeof(float) * n_samples
;
329 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
330 struct impl_libsamplerate
*u
;
331 unsigned in_n_samples
, out_n_samples
, n_frames
;
338 pa_assert(input
->memblock
);
340 pa_assert(r
->impl_data
);
343 /* Remap channels and place the result int buf2 */
345 if (!u
->map_required
|| !input
->length
)
348 in_n_samples
= input
->length
/ sizeof(float);
349 n_frames
= in_n_samples
/ r
->i_ss
.channels
;
350 out_n_samples
= n_frames
* r
->o_ss
.channels
;
352 if (!u
->buf2
.memblock
|| u
->buf2_samples
< out_n_samples
) {
353 if (u
->buf2
.memblock
)
354 pa_memblock_unref(u
->buf2
.memblock
);
356 u
->buf2_samples
= out_n_samples
;
357 u
->buf2
.memblock
= pa_memblock_new(r
->mempool
, u
->buf2
.length
= sizeof(float) * out_n_samples
);
361 src
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
362 dst
= (float*) pa_memblock_acquire(u
->buf2
.memblock
);
364 memset(dst
, 0, u
->buf2
.length
);
366 o_skip
= sizeof(float) * r
->o_ss
.channels
;
367 i_skip
= sizeof(float) * r
->i_ss
.channels
;
369 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
371 static const float one
= 1.0;
373 for (i
= 0; i
< PA_CHANNELS_MAX
&& u
->map_table
[oc
][i
] >= 0; i
++)
377 src
+ u
->map_table
[oc
][i
], i_skip
,
382 pa_memblock_release(input
->memblock
);
383 pa_memblock_release(u
->buf2
.memblock
);
385 u
->buf2
.length
= out_n_samples
* sizeof(float);
390 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
391 struct impl_libsamplerate
*u
;
393 unsigned in_n_frames
, in_n_samples
;
394 unsigned out_n_frames
, out_n_samples
;
399 pa_assert(r
->impl_data
);
402 /* Resample the data and place the result in buf3 */
404 if (!u
->src_state
|| !input
->length
)
407 in_n_samples
= input
->length
/ sizeof(float);
408 in_n_frames
= in_n_samples
/ r
->o_ss
.channels
;
410 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+1024;
411 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
413 if (!u
->buf3
.memblock
|| u
->buf3_samples
< out_n_samples
) {
414 if (u
->buf3
.memblock
)
415 pa_memblock_unref(u
->buf3
.memblock
);
417 u
->buf3_samples
= out_n_samples
;
418 u
->buf3
.memblock
= pa_memblock_new(r
->mempool
, u
->buf3
.length
= sizeof(float) * out_n_samples
);
422 data
.data_in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
423 data
.input_frames
= in_n_frames
;
425 data
.data_out
= (float*) pa_memblock_acquire(u
->buf3
.memblock
);
426 data
.output_frames
= out_n_frames
;
428 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
429 data
.end_of_input
= 0;
431 ret
= src_process(u
->src_state
, &data
);
433 pa_assert((unsigned) data
.input_frames_used
== in_n_frames
);
435 pa_memblock_release(input
->memblock
);
436 pa_memblock_release(u
->buf3
.memblock
);
438 u
->buf3
.length
= data
.output_frames_gen
* sizeof(float) * r
->o_ss
.channels
;
443 static pa_memchunk
*convert_from_float(pa_resampler
*r
, pa_memchunk
*input
) {
444 struct impl_libsamplerate
*u
;
445 unsigned n_samples
, n_frames
;
450 pa_assert(r
->impl_data
);
453 /* Convert the data into the correct sample type and place the result in buf4 */
455 if (!u
->from_float32ne_func
|| !input
->length
)
458 n_frames
= input
->length
/ sizeof(float) / r
->o_ss
.channels
;
459 n_samples
= n_frames
* r
->o_ss
.channels
;
461 if (!u
->buf4
.memblock
|| u
->buf4_samples
< n_samples
) {
462 if (u
->buf4
.memblock
)
463 pa_memblock_unref(u
->buf4
.memblock
);
465 u
->buf4_samples
= n_samples
;
466 u
->buf4
.memblock
= pa_memblock_new(r
->mempool
, u
->buf4
.length
= r
->o_fz
* n_frames
);
470 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
471 dst
= pa_memblock_acquire(u
->buf4
.memblock
);
472 u
->from_float32ne_func(n_samples
, src
, dst
);
473 pa_memblock_release(input
->memblock
);
474 pa_memblock_release(u
->buf4
.memblock
);
476 u
->buf4
.length
= r
->o_fz
* n_frames
;
481 static void libsamplerate_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
482 struct impl_libsamplerate
*u
;
488 pa_assert(in
->length
);
489 pa_assert(in
->memblock
);
490 pa_assert(in
->length
% r
->i_fz
== 0);
491 pa_assert(r
->impl_data
);
495 buf
= (pa_memchunk
*) in
;
496 buf
= convert_to_float(r
, buf
);
497 buf
= remap_channels(r
, buf
);
498 buf
= resample(r
, buf
);
501 buf
= convert_from_float(r
, buf
);
505 pa_memblock_ref(buf
->memblock
);
507 pa_memchunk_reset(buf
);
509 pa_memchunk_reset(out
);
512 static void libsamplerate_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
513 struct impl_libsamplerate
*u
;
517 pa_assert(r
->impl_data
);
522 u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
);
523 pa_assert(u
->src_state
);
525 int ret
= src_set_ratio(u
->src_state
, (double) r
->o_ss
.rate
/ rate
);
530 static void libsamplerate_update_output_rate(pa_resampler
*r
, uint32_t rate
) {
531 struct impl_libsamplerate
*u
;
535 pa_assert(r
->impl_data
);
540 u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
);
541 pa_assert(u
->src_state
);
543 int ret
= src_set_ratio(u
->src_state
, (double) rate
/ r
->i_ss
.rate
);
548 static int libsamplerate_init(pa_resampler
*r
) {
549 struct impl_libsamplerate
*u
= NULL
;
552 r
->impl_data
= u
= pa_xnew(struct impl_libsamplerate
, 1);
554 pa_memchunk_reset(&u
->buf1
);
555 pa_memchunk_reset(&u
->buf2
);
556 pa_memchunk_reset(&u
->buf3
);
557 pa_memchunk_reset(&u
->buf4
);
558 u
->buf1_samples
= u
->buf2_samples
= u
->buf3_samples
= u
->buf4_samples
= 0;
560 if (r
->i_ss
.format
== PA_SAMPLE_FLOAT32NE
)
561 u
->to_float32ne_func
= NULL
;
562 else if (!(u
->to_float32ne_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
565 if (r
->o_ss
.format
== PA_SAMPLE_FLOAT32NE
)
566 u
->from_float32ne_func
= NULL
;
567 else if (!(u
->from_float32ne_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
570 if (r
->o_ss
.rate
== r
->i_ss
.rate
)
572 else if (!(u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
575 r
->impl_free
= libsamplerate_free
;
576 r
->impl_update_input_rate
= libsamplerate_update_input_rate
;
577 r
->impl_update_output_rate
= libsamplerate_update_output_rate
;
578 r
->impl_run
= libsamplerate_run
;
589 /* Trivial implementation */
591 static void trivial_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
594 struct impl_trivial
*u
;
599 pa_assert(r
->impl_data
);
604 pa_assert(fz
== r
->o_fz
);
606 n_frames
= in
->length
/fz
;
608 if (r
->i_ss
.rate
== r
->o_ss
.rate
) {
610 /* In case there's no diefference in sample types, do nothing */
612 pa_memblock_ref(out
->memblock
);
614 u
->o_counter
+= n_frames
;
616 /* Do real resampling */
621 /* The length of the new memory block rounded up */
622 l
= ((((n_frames
+1) * r
->o_ss
.rate
) / r
->i_ss
.rate
) + 1) * fz
;
625 out
->memblock
= pa_memblock_new(r
->mempool
, l
);
627 src
= (uint8_t*) pa_memblock_acquire(in
->memblock
) + in
->index
;
628 dst
= pa_memblock_acquire(out
->memblock
);
630 for (o_index
= 0;; o_index
++, u
->o_counter
++) {
633 j
= (u
->o_counter
* r
->i_ss
.rate
/ r
->o_ss
.rate
);
634 j
= j
> u
->i_counter
? j
- u
->i_counter
: 0;
639 pa_assert(o_index
*fz
< pa_memblock_get_length(out
->memblock
));
641 memcpy((uint8_t*) dst
+ fz
*o_index
,
642 (uint8_t*) src
+ fz
*j
, fz
);
646 pa_memblock_release(in
->memblock
);
647 pa_memblock_release(out
->memblock
);
649 out
->length
= o_index
*fz
;
652 u
->i_counter
+= n_frames
;
654 /* Normalize counters */
655 while (u
->i_counter
>= r
->i_ss
.rate
) {
656 u
->i_counter
-= r
->i_ss
.rate
;
657 pa_assert(u
->o_counter
>= r
->o_ss
.rate
);
658 u
->o_counter
-= r
->o_ss
.rate
;
662 static void trivial_free(pa_resampler
*r
) {
665 pa_xfree(r
->impl_data
);
668 static void trivial_update_rate(pa_resampler
*r
, uint32_t rate
) {
669 struct impl_trivial
*u
;
673 pa_assert(r
->impl_data
);
680 static int trivial_init(pa_resampler
*r
) {
681 struct impl_trivial
*u
;
684 pa_assert(r
->i_ss
.format
== r
->o_ss
.format
);
685 pa_assert(r
->i_ss
.channels
== r
->o_ss
.channels
);
687 r
->impl_data
= u
= pa_xnew(struct impl_trivial
, 1);
688 u
->o_counter
= u
->i_counter
= 0;
690 r
->impl_run
= trivial_run
;
691 r
->impl_free
= trivial_free
;
692 r
->impl_update_input_rate
= trivial_update_rate
;
693 r
->impl_update_output_rate
= trivial_update_rate
;