4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <samplerate.h>
30 #include <liboil/liboilfuncs.h>
31 #include <liboil/liboil.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/sconv.h>
36 #include <pulsecore/log.h>
38 #include "resampler.h"
41 pa_resample_method_t resample_method
;
42 pa_sample_spec i_ss
, o_ss
;
43 pa_channel_map i_cm
, o_cm
;
47 void (*impl_free
)(pa_resampler
*r
);
48 void (*impl_update_input_rate
)(pa_resampler
*r
, uint32_t rate
);
49 void (*impl_run
)(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
);
53 struct impl_libsamplerate
{
54 pa_memchunk buf1
, buf2
, buf3
, buf4
;
55 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
57 pa_convert_to_float32ne_func_t to_float32ne_func
;
58 pa_convert_from_float32ne_func_t from_float32ne_func
;
61 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
70 static int libsamplerate_init(pa_resampler
*r
);
71 static int trivial_init(pa_resampler
*r
);
73 pa_resampler
* pa_resampler_new(
75 const pa_sample_spec
*a
,
76 const pa_channel_map
*am
,
77 const pa_sample_spec
*b
,
78 const pa_channel_map
*bm
,
79 pa_resample_method_t resample_method
) {
81 pa_resampler
*r
= NULL
;
86 assert(pa_sample_spec_valid(a
));
87 assert(pa_sample_spec_valid(b
));
88 assert(resample_method
!= PA_RESAMPLER_INVALID
);
90 r
= pa_xnew(pa_resampler
, 1);
93 r
->resample_method
= resample_method
;
96 r
->impl_update_input_rate
= NULL
;
99 /* Fill sample specs */
106 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
111 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
113 r
->i_fz
= pa_frame_size(a
);
114 r
->o_fz
= pa_frame_size(b
);
116 /* Choose implementation */
117 if (a
->channels
!= b
->channels
||
118 a
->format
!= b
->format
||
119 !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) ||
120 resample_method
!= PA_RESAMPLER_TRIVIAL
) {
122 /* Use the libsamplerate based resampler for the complicated cases */
123 if (resample_method
== PA_RESAMPLER_TRIVIAL
)
124 r
->resample_method
= PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
;
126 if (libsamplerate_init(r
) < 0)
130 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
131 if (trivial_init(r
) < 0)
144 void pa_resampler_free(pa_resampler
*r
) {
153 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
157 if (r
->i_ss
.rate
== rate
)
162 if (r
->impl_update_input_rate
)
163 r
->impl_update_input_rate(r
, rate
);
166 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
167 assert(r
&& in
&& out
&& r
->impl_run
);
169 r
->impl_run(r
, in
, out
);
172 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
175 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
178 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
180 return r
->resample_method
;
183 static const char * const resample_methods
[] = {
184 "src-sinc-best-quality",
185 "src-sinc-medium-quality",
187 "src-zero-order-hold",
192 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
194 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
197 return resample_methods
[m
];
200 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
201 pa_resample_method_t m
;
205 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
206 if (!strcmp(string
, resample_methods
[m
]))
209 return PA_RESAMPLER_INVALID
;
213 /*** libsamplerate based implementation ***/
215 static void libsamplerate_free(pa_resampler
*r
) {
216 struct impl_libsamplerate
*u
;
219 assert(r
->impl_data
);
224 src_delete(u
->src_state
);
226 if (u
->buf1
.memblock
)
227 pa_memblock_unref(u
->buf1
.memblock
);
228 if (u
->buf2
.memblock
)
229 pa_memblock_unref(u
->buf2
.memblock
);
230 if (u
->buf3
.memblock
)
231 pa_memblock_unref(u
->buf3
.memblock
);
232 if (u
->buf4
.memblock
)
233 pa_memblock_unref(u
->buf4
.memblock
);
237 static void calc_map_table(pa_resampler
*r
) {
238 struct impl_libsamplerate
*u
;
241 assert(r
->impl_data
);
245 if (!(u
->map_required
= (!pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) || r
->i_ss
.channels
!= r
->o_ss
.channels
)))
248 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
251 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
252 pa_channel_position_t a
, b
;
258 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
259 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
260 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
261 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
263 u
->map_table
[oc
][i
++] = ic
;
266 /* Add an end marker */
267 if (i
< PA_CHANNELS_MAX
)
268 u
->map_table
[oc
][i
] = -1;
272 static pa_memchunk
* convert_to_float(pa_resampler
*r
, pa_memchunk
*input
) {
273 struct impl_libsamplerate
*u
;
279 assert(input
->memblock
);
281 assert(r
->impl_data
);
284 /* Convert the incoming sample into floats and place them in buf1 */
286 if (!u
->to_float32ne_func
|| !input
->length
)
289 n_samples
= (input
->length
/ r
->i_fz
) * r
->i_ss
.channels
;
291 if (!u
->buf1
.memblock
|| u
->buf1_samples
< n_samples
) {
292 if (u
->buf1
.memblock
)
293 pa_memblock_unref(u
->buf1
.memblock
);
295 u
->buf1_samples
= n_samples
;
296 u
->buf1
.memblock
= pa_memblock_new(r
->mempool
, u
->buf1
.length
= sizeof(float) * n_samples
);
300 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
;
301 dst
= (uint8_t*) pa_memblock_acquire(u
->buf1
.memblock
);
302 u
->to_float32ne_func(n_samples
, src
, dst
);
303 pa_memblock_release(input
->memblock
);
304 pa_memblock_release(u
->buf1
.memblock
);
306 u
->buf1
.length
= sizeof(float) * n_samples
;
311 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
312 struct impl_libsamplerate
*u
;
313 unsigned n_samples
, n_frames
;
320 assert(input
->memblock
);
322 assert(r
->impl_data
);
325 /* Remap channels and place the result int buf2 */
327 if (!u
->map_required
|| !input
->length
)
330 n_samples
= input
->length
/ sizeof(float);
331 n_frames
= n_samples
/ r
->o_ss
.channels
;
333 if (!u
->buf2
.memblock
|| u
->buf2_samples
< n_samples
) {
334 if (u
->buf2
.memblock
)
335 pa_memblock_unref(u
->buf2
.memblock
);
337 u
->buf2_samples
= n_samples
;
338 u
->buf2
.memblock
= pa_memblock_new(r
->mempool
, u
->buf2
.length
= sizeof(float) * n_samples
);
342 src
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
343 dst
= (float*) pa_memblock_acquire(u
->buf2
.memblock
);
345 memset(dst
, 0, n_samples
* sizeof(float));
347 o_skip
= sizeof(float) * r
->o_ss
.channels
;
348 i_skip
= sizeof(float) * r
->i_ss
.channels
;
350 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
352 static const float one
= 1.0;
354 for (i
= 0; i
< PA_CHANNELS_MAX
&& u
->map_table
[oc
][i
] >= 0; i
++)
358 src
+ u
->map_table
[oc
][i
], i_skip
,
363 pa_memblock_release(input
->memblock
);
364 pa_memblock_release(u
->buf2
.memblock
);
366 u
->buf2
.length
= n_frames
* sizeof(float) * r
->o_ss
.channels
;
371 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
372 struct impl_libsamplerate
*u
;
374 unsigned in_n_frames
, in_n_samples
;
375 unsigned out_n_frames
, out_n_samples
;
380 assert(r
->impl_data
);
383 /* Resample the data and place the result in buf3 */
385 if (!u
->src_state
|| !input
->length
)
388 in_n_samples
= input
->length
/ sizeof(float);
389 in_n_frames
= in_n_samples
* r
->o_ss
.channels
;
391 out_n_frames
= (in_n_frames
*r
->o_ss
.rate
/r
->i_ss
.rate
)+1024;
392 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
394 if (!u
->buf3
.memblock
|| u
->buf3_samples
< out_n_samples
) {
395 if (u
->buf3
.memblock
)
396 pa_memblock_unref(u
->buf3
.memblock
);
398 u
->buf3_samples
= out_n_samples
;
399 u
->buf3
.memblock
= pa_memblock_new(r
->mempool
, u
->buf3
.length
= sizeof(float) * out_n_samples
);
403 data
.data_in
= (float*) ((uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->index
);
404 data
.input_frames
= in_n_frames
;
406 data
.data_out
= (float*) pa_memblock_acquire(u
->buf3
.memblock
);
407 data
.output_frames
= out_n_frames
;
409 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
410 data
.end_of_input
= 0;
412 ret
= src_process(u
->src_state
, &data
);
414 assert((unsigned) data
.input_frames_used
== in_n_frames
);
416 pa_memblock_release(input
->memblock
);
417 pa_memblock_release(u
->buf3
.memblock
);
419 u
->buf3
.length
= data
.output_frames_gen
* sizeof(float) * r
->o_ss
.channels
;
424 static pa_memchunk
*convert_from_float(pa_resampler
*r
, pa_memchunk
*input
) {
425 struct impl_libsamplerate
*u
;
426 unsigned n_samples
, n_frames
;
431 assert(r
->impl_data
);
434 /* Convert the data into the correct sample type and place the result in buf4 */
436 if (!u
->from_float32ne_func
|| !input
->length
)
439 n_frames
= input
->length
/ sizeof(float) / r
->o_ss
.channels
;
440 n_samples
= n_frames
* r
->o_ss
.channels
;
442 if (u
->buf4_samples
< n_samples
) {
443 if (u
->buf4
.memblock
)
444 pa_memblock_unref(u
->buf4
.memblock
);
446 u
->buf4_samples
= n_samples
;
447 u
->buf4
.memblock
= pa_memblock_new(r
->mempool
, u
->buf4
.length
= r
->o_fz
* n_frames
);
451 src
= (uint8_t*) pa_memblock_acquire(input
->memblock
) + input
->length
;
452 dst
= pa_memblock_acquire(u
->buf4
.memblock
);
453 u
->from_float32ne_func(n_samples
, src
, dst
);
454 pa_memblock_release(input
->memblock
);
455 pa_memblock_release(u
->buf4
.memblock
);
457 u
->buf4
.length
= r
->o_fz
* n_frames
;
462 static void libsamplerate_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
463 struct impl_libsamplerate
*u
;
470 assert(in
->memblock
);
471 assert(in
->length
% r
->i_fz
== 0);
472 assert(r
->impl_data
);
476 buf
= convert_to_float(r
, (pa_memchunk
*) in
);
477 buf
= remap_channels(r
, buf
);
478 buf
= resample(r
, buf
);
481 buf
= convert_from_float(r
, buf
);
485 pa_memblock_ref(buf
->memblock
);
487 pa_memchunk_reset(buf
);
489 pa_memchunk_reset(out
);
491 pa_memblock_release(in
->memblock
);
495 static void libsamplerate_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
496 struct impl_libsamplerate
*u
;
500 assert(r
->impl_data
);
505 u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
);
506 assert(u
->src_state
);
508 int ret
= src_set_ratio(u
->src_state
, (double) r
->o_ss
.rate
/ rate
);
513 static int libsamplerate_init(pa_resampler
*r
) {
514 struct impl_libsamplerate
*u
= NULL
;
517 r
->impl_data
= u
= pa_xnew(struct impl_libsamplerate
, 1);
519 pa_memchunk_reset(&u
->buf1
);
520 pa_memchunk_reset(&u
->buf2
);
521 pa_memchunk_reset(&u
->buf3
);
522 pa_memchunk_reset(&u
->buf4
);
523 u
->buf1_samples
= u
->buf2_samples
= u
->buf3_samples
= u
->buf4_samples
= 0;
525 if (r
->i_ss
.format
== PA_SAMPLE_FLOAT32NE
)
526 u
->to_float32ne_func
= NULL
;
527 else if (!(u
->to_float32ne_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
530 if (r
->o_ss
.format
== PA_SAMPLE_FLOAT32NE
)
531 u
->from_float32ne_func
= NULL
;
532 else if (!(u
->from_float32ne_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
535 if (r
->o_ss
.rate
== r
->i_ss
.rate
)
537 else if (!(u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
540 r
->impl_free
= libsamplerate_free
;
541 r
->impl_update_input_rate
= libsamplerate_update_input_rate
;
542 r
->impl_run
= libsamplerate_run
;
553 /* Trivial implementation */
555 static void trivial_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
558 struct impl_trivial
*u
;
563 assert(r
->impl_data
);
568 assert(fz
== r
->o_fz
);
570 n_frames
= in
->length
/fz
;
572 if (r
->i_ss
.rate
== r
->o_ss
.rate
) {
574 /* In case there's no diefference in sample types, do nothing */
576 pa_memblock_ref(out
->memblock
);
578 u
->o_counter
+= n_frames
;
580 /* Do real resampling */
585 /* The length of the new memory block rounded up */
586 l
= ((((n_frames
+1) * r
->o_ss
.rate
) / r
->i_ss
.rate
) + 1) * fz
;
589 out
->memblock
= pa_memblock_new(r
->mempool
, l
);
591 src
= (uint8_t*) pa_memblock_acquire(in
->memblock
) + in
->index
;
592 dst
= pa_memblock_acquire(out
->memblock
);
594 for (o_index
= 0;; o_index
++, u
->o_counter
++) {
597 j
= (u
->o_counter
* r
->i_ss
.rate
/ r
->o_ss
.rate
);
598 j
= j
> u
->i_counter
? j
- u
->i_counter
: 0;
603 assert(o_index
*fz
< pa_memblock_get_length(out
->memblock
));
605 memcpy((uint8_t*) dst
+ fz
*o_index
,
606 (uint8_t*) src
+ fz
*j
, fz
);
610 pa_memblock_release(in
->memblock
);
611 pa_memblock_release(out
->memblock
);
613 out
->length
= o_index
*fz
;
616 u
->i_counter
+= n_frames
;
618 /* Normalize counters */
619 while (u
->i_counter
>= r
->i_ss
.rate
) {
620 u
->i_counter
-= r
->i_ss
.rate
;
621 assert(u
->o_counter
>= r
->o_ss
.rate
);
622 u
->o_counter
-= r
->o_ss
.rate
;
626 static void trivial_free(pa_resampler
*r
) {
629 pa_xfree(r
->impl_data
);
632 static void trivial_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
633 struct impl_trivial
*u
;
637 assert(r
->impl_data
);
644 static int trivial_init(pa_resampler
*r
) {
645 struct impl_trivial
*u
;
648 assert(r
->i_ss
.format
== r
->o_ss
.format
);
649 assert(r
->i_ss
.channels
== r
->o_ss
.channels
);
651 r
->impl_data
= u
= pa_xnew(struct impl_trivial
, 1);
652 u
->o_counter
= u
->i_counter
= 0;
654 r
->impl_run
= trivial_run
;
655 r
->impl_free
= trivial_free
;
656 r
->impl_update_input_rate
= trivial_update_input_rate
;