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
31 #include <samplerate.h>
32 #include <liboil/liboilfuncs.h>
33 #include <liboil/liboil.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
40 #include "resampler.h"
43 pa_resample_method_t resample_method
;
44 pa_sample_spec i_ss
, o_ss
;
45 pa_channel_map i_cm
, o_cm
;
49 void (*impl_free
)(pa_resampler
*r
);
50 void (*impl_update_input_rate
)(pa_resampler
*r
, uint32_t rate
);
51 void (*impl_update_output_rate
)(pa_resampler
*r
, uint32_t rate
);
52 void (*impl_run
)(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
);
56 struct impl_libsamplerate
{
57 pa_memchunk buf1
, buf2
, buf3
, buf4
;
58 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
60 pa_convert_to_float32ne_func_t to_float32ne_func
;
61 pa_convert_from_float32ne_func_t from_float32ne_func
;
64 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
73 static int libsamplerate_init(pa_resampler
*r
);
74 static int trivial_init(pa_resampler
*r
);
76 pa_resampler
* pa_resampler_new(
78 const pa_sample_spec
*a
,
79 const pa_channel_map
*am
,
80 const pa_sample_spec
*b
,
81 const pa_channel_map
*bm
,
82 pa_resample_method_t resample_method
) {
84 pa_resampler
*r
= NULL
;
89 assert(pa_sample_spec_valid(a
));
90 assert(pa_sample_spec_valid(b
));
91 assert(resample_method
!= PA_RESAMPLER_INVALID
);
93 r
= pa_xnew(pa_resampler
, 1);
96 r
->resample_method
= resample_method
;
99 r
->impl_update_input_rate
= NULL
;
102 /* Fill sample specs */
109 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
114 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
116 r
->i_fz
= pa_frame_size(a
);
117 r
->o_fz
= pa_frame_size(b
);
119 /* Choose implementation */
120 if (a
->channels
!= b
->channels
||
121 a
->format
!= b
->format
||
122 !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) ||
123 resample_method
!= PA_RESAMPLER_TRIVIAL
) {
125 /* Use the libsamplerate based resampler for the complicated cases */
126 if (resample_method
== PA_RESAMPLER_TRIVIAL
)
127 r
->resample_method
= PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
;
129 if (libsamplerate_init(r
) < 0)
133 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
134 if (trivial_init(r
) < 0)
147 void pa_resampler_free(pa_resampler
*r
) {
156 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
160 if (r
->i_ss
.rate
== rate
)
165 if (r
->impl_update_input_rate
)
166 r
->impl_update_input_rate(r
, rate
);
169 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
173 if (r
->o_ss
.rate
== rate
)
178 if (r
->impl_update_output_rate
)
179 r
->impl_update_output_rate(r
, rate
);
182 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
183 assert(r
&& in
&& out
&& r
->impl_run
);
185 r
->impl_run(r
, in
, out
);
188 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
191 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
194 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
196 return r
->resample_method
;
199 static const char * const resample_methods
[] = {
200 "src-sinc-best-quality",
201 "src-sinc-medium-quality",
203 "src-zero-order-hold",
208 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
210 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
213 return resample_methods
[m
];
216 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
217 pa_resample_method_t m
;
221 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
222 if (!strcmp(string
, resample_methods
[m
]))
225 return PA_RESAMPLER_INVALID
;
229 /*** libsamplerate based implementation ***/
231 static void libsamplerate_free(pa_resampler
*r
) {
232 struct impl_libsamplerate
*u
;
235 assert(r
->impl_data
);
240 src_delete(u
->src_state
);
242 if (u
->buf1
.memblock
)
243 pa_memblock_unref(u
->buf1
.memblock
);
244 if (u
->buf2
.memblock
)
245 pa_memblock_unref(u
->buf2
.memblock
);
246 if (u
->buf3
.memblock
)
247 pa_memblock_unref(u
->buf3
.memblock
);
248 if (u
->buf4
.memblock
)
249 pa_memblock_unref(u
->buf4
.memblock
);
253 static void calc_map_table(pa_resampler
*r
) {
254 struct impl_libsamplerate
*u
;
257 assert(r
->impl_data
);
261 if (!(u
->map_required
= (!pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) || r
->i_ss
.channels
!= r
->o_ss
.channels
)))
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 assert(input
->memblock
);
297 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
);
318 u
->to_float32ne_func(n_samples
, src
, dst
);
319 pa_memblock_release(input
->memblock
);
320 pa_memblock_release(u
->buf1
.memblock
);
322 u
->buf1
.length
= sizeof(float) * n_samples
;
327 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
328 struct impl_libsamplerate
*u
;
329 unsigned n_samples
, n_frames
;
336 assert(input
->memblock
);
338 assert(r
->impl_data
);
341 /* Remap channels and place the result int buf2 */
343 if (!u
->map_required
|| !input
->length
)
346 n_samples
= input
->length
/ sizeof(float);
347 n_frames
= n_samples
/ r
->o_ss
.channels
;
349 if (!u
->buf2
.memblock
|| u
->buf2_samples
< n_samples
) {
350 if (u
->buf2
.memblock
)
351 pa_memblock_unref(u
->buf2
.memblock
);
353 u
->buf2_samples
= n_samples
;
354 u
->buf2
.memblock
= pa_memblock_new(r
->mempool
, u
->buf2
.length
= sizeof(float) * n_samples
);
358 src
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
359 dst
= (float*) pa_memblock_acquire(u
->buf2
.memblock
);
361 memset(dst
, 0, n_samples
* sizeof(float));
363 o_skip
= sizeof(float) * r
->o_ss
.channels
;
364 i_skip
= sizeof(float) * r
->i_ss
.channels
;
366 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
368 static const float one
= 1.0;
370 for (i
= 0; i
< PA_CHANNELS_MAX
&& u
->map_table
[oc
][i
] >= 0; i
++)
374 src
+ u
->map_table
[oc
][i
], i_skip
,
379 pa_memblock_release(input
->memblock
);
380 pa_memblock_release(u
->buf2
.memblock
);
382 u
->buf2
.length
= n_frames
* sizeof(float) * r
->o_ss
.channels
;
387 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
388 struct impl_libsamplerate
*u
;
390 unsigned in_n_frames
, in_n_samples
;
391 unsigned out_n_frames
, out_n_samples
;
396 assert(r
->impl_data
);
399 /* Resample the data and place the result in buf3 */
401 if (!u
->src_state
|| !input
->length
)
404 in_n_samples
= input
->length
/ sizeof(float);
405 in_n_frames
= in_n_samples
* r
->o_ss
.channels
;
407 out_n_frames
= (in_n_frames
*r
->o_ss
.rate
/r
->i_ss
.rate
)+1024;
408 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
410 if (!u
->buf3
.memblock
|| u
->buf3_samples
< out_n_samples
) {
411 if (u
->buf3
.memblock
)
412 pa_memblock_unref(u
->buf3
.memblock
);
414 u
->buf3_samples
= out_n_samples
;
415 u
->buf3
.memblock
= pa_memblock_new(r
->mempool
, u
->buf3
.length
= sizeof(float) * out_n_samples
);
419 data
.data_in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
420 data
.input_frames
= in_n_frames
;
422 data
.data_out
= (float*) pa_memblock_acquire(u
->buf3
.memblock
);
423 data
.output_frames
= out_n_frames
;
425 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
426 data
.end_of_input
= 0;
428 ret
= src_process(u
->src_state
, &data
);
430 assert((unsigned) data
.input_frames_used
== in_n_frames
);
432 pa_memblock_release(input
->memblock
);
433 pa_memblock_release(u
->buf3
.memblock
);
435 u
->buf3
.length
= data
.output_frames_gen
* sizeof(float) * r
->o_ss
.channels
;
440 static pa_memchunk
*convert_from_float(pa_resampler
*r
, pa_memchunk
*input
) {
441 struct impl_libsamplerate
*u
;
442 unsigned n_samples
, n_frames
;
447 assert(r
->impl_data
);
450 /* Convert the data into the correct sample type and place the result in buf4 */
452 if (!u
->from_float32ne_func
|| !input
->length
)
455 n_frames
= input
->length
/ sizeof(float) / r
->o_ss
.channels
;
456 n_samples
= n_frames
* r
->o_ss
.channels
;
458 if (!u
->buf4
.memblock
|| u
->buf4_samples
< n_samples
) {
459 if (u
->buf4
.memblock
)
460 pa_memblock_unref(u
->buf4
.memblock
);
462 u
->buf4_samples
= n_samples
;
463 u
->buf4
.memblock
= pa_memblock_new(r
->mempool
, u
->buf4
.length
= r
->o_fz
* n_frames
);
467 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->length
;
468 dst
= pa_memblock_acquire(u
->buf4
.memblock
);
469 u
->from_float32ne_func(n_samples
, src
, dst
);
470 pa_memblock_release(input
->memblock
);
471 pa_memblock_release(u
->buf4
.memblock
);
473 u
->buf4
.length
= r
->o_fz
* n_frames
;
478 static void libsamplerate_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
479 struct impl_libsamplerate
*u
;
486 assert(in
->memblock
);
487 assert(in
->length
% r
->i_fz
== 0);
488 assert(r
->impl_data
);
492 buf
= (pa_memchunk
*) in
;
493 buf
= convert_to_float(r
, buf
);
494 buf
= remap_channels(r
, buf
);
495 buf
= resample(r
, buf
);
498 buf
= convert_from_float(r
, buf
);
502 pa_memblock_ref(buf
->memblock
);
504 pa_memchunk_reset(buf
);
506 pa_memchunk_reset(out
);
509 static void libsamplerate_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
510 struct impl_libsamplerate
*u
;
514 assert(r
->impl_data
);
519 u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
);
520 assert(u
->src_state
);
522 int ret
= src_set_ratio(u
->src_state
, (double) r
->o_ss
.rate
/ rate
);
527 static void libsamplerate_update_output_rate(pa_resampler
*r
, uint32_t rate
) {
528 struct impl_libsamplerate
*u
;
532 assert(r
->impl_data
);
537 u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
);
538 assert(u
->src_state
);
540 int ret
= src_set_ratio(u
->src_state
, (double) rate
/ r
->i_ss
.rate
);
545 static int libsamplerate_init(pa_resampler
*r
) {
546 struct impl_libsamplerate
*u
= NULL
;
549 r
->impl_data
= u
= pa_xnew(struct impl_libsamplerate
, 1);
551 pa_memchunk_reset(&u
->buf1
);
552 pa_memchunk_reset(&u
->buf2
);
553 pa_memchunk_reset(&u
->buf3
);
554 pa_memchunk_reset(&u
->buf4
);
555 u
->buf1_samples
= u
->buf2_samples
= u
->buf3_samples
= u
->buf4_samples
= 0;
557 if (r
->i_ss
.format
== PA_SAMPLE_FLOAT32NE
)
558 u
->to_float32ne_func
= NULL
;
559 else if (!(u
->to_float32ne_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
562 if (r
->o_ss
.format
== PA_SAMPLE_FLOAT32NE
)
563 u
->from_float32ne_func
= NULL
;
564 else if (!(u
->from_float32ne_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
567 if (r
->o_ss
.rate
== r
->i_ss
.rate
)
569 else if (!(u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
572 r
->impl_free
= libsamplerate_free
;
573 r
->impl_update_input_rate
= libsamplerate_update_input_rate
;
574 r
->impl_update_output_rate
= libsamplerate_update_output_rate
;
575 r
->impl_run
= libsamplerate_run
;
586 /* Trivial implementation */
588 static void trivial_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
591 struct impl_trivial
*u
;
596 assert(r
->impl_data
);
601 assert(fz
== r
->o_fz
);
603 n_frames
= in
->length
/fz
;
605 if (r
->i_ss
.rate
== r
->o_ss
.rate
) {
607 /* In case there's no diefference in sample types, do nothing */
609 pa_memblock_ref(out
->memblock
);
611 u
->o_counter
+= n_frames
;
613 /* Do real resampling */
618 /* The length of the new memory block rounded up */
619 l
= ((((n_frames
+1) * r
->o_ss
.rate
) / r
->i_ss
.rate
) + 1) * fz
;
622 out
->memblock
= pa_memblock_new(r
->mempool
, l
);
624 src
= (uint8_t*) pa_memblock_acquire(in
->memblock
) + in
->index
;
625 dst
= pa_memblock_acquire(out
->memblock
);
627 for (o_index
= 0;; o_index
++, u
->o_counter
++) {
630 j
= (u
->o_counter
* r
->i_ss
.rate
/ r
->o_ss
.rate
);
631 j
= j
> u
->i_counter
? j
- u
->i_counter
: 0;
636 assert(o_index
*fz
< pa_memblock_get_length(out
->memblock
));
638 memcpy((uint8_t*) dst
+ fz
*o_index
,
639 (uint8_t*) src
+ fz
*j
, fz
);
643 pa_memblock_release(in
->memblock
);
644 pa_memblock_release(out
->memblock
);
646 out
->length
= o_index
*fz
;
649 u
->i_counter
+= n_frames
;
651 /* Normalize counters */
652 while (u
->i_counter
>= r
->i_ss
.rate
) {
653 u
->i_counter
-= r
->i_ss
.rate
;
654 assert(u
->o_counter
>= r
->o_ss
.rate
);
655 u
->o_counter
-= r
->o_ss
.rate
;
659 static void trivial_free(pa_resampler
*r
) {
662 pa_xfree(r
->impl_data
);
665 static void trivial_update_rate(pa_resampler
*r
, uint32_t rate
) {
666 struct impl_trivial
*u
;
670 assert(r
->impl_data
);
677 static int trivial_init(pa_resampler
*r
) {
678 struct impl_trivial
*u
;
681 assert(r
->i_ss
.format
== r
->o_ss
.format
);
682 assert(r
->i_ss
.channels
== r
->o_ss
.channels
);
684 r
->impl_data
= u
= pa_xnew(struct impl_trivial
, 1);
685 u
->o_counter
= u
->i_counter
= 0;
687 r
->impl_run
= trivial_run
;
688 r
->impl_free
= trivial_free
;
689 r
->impl_update_input_rate
= trivial_update_rate
;
690 r
->impl_update_output_rate
= trivial_update_rate
;