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_memblock
*buf1_block
, *buf2_block
, *buf3_block
, *buf4_block
;
55 float* buf1
, *buf2
, *buf3
, *buf4
;
56 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
58 pa_convert_to_float32ne_func_t to_float32ne_func
;
59 pa_convert_from_float32ne_func_t from_float32ne_func
;
62 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
71 static int libsamplerate_init(pa_resampler
*r
);
72 static int trivial_init(pa_resampler
*r
);
74 pa_resampler
* pa_resampler_new(
76 const pa_sample_spec
*a
,
77 const pa_channel_map
*am
,
78 const pa_sample_spec
*b
,
79 const pa_channel_map
*bm
,
80 pa_resample_method_t resample_method
) {
82 pa_resampler
*r
= NULL
;
87 assert(pa_sample_spec_valid(a
));
88 assert(pa_sample_spec_valid(b
));
89 assert(resample_method
!= PA_RESAMPLER_INVALID
);
91 r
= pa_xnew(pa_resampler
, 1);
94 r
->resample_method
= resample_method
;
97 r
->impl_update_input_rate
= NULL
;
100 /* Fill sample specs */
107 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
112 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
114 r
->i_fz
= pa_frame_size(a
);
115 r
->o_fz
= pa_frame_size(b
);
117 /* Choose implementation */
118 if (a
->channels
!= b
->channels
||
119 a
->format
!= b
->format
||
120 !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) ||
121 resample_method
!= PA_RESAMPLER_TRIVIAL
) {
123 /* Use the libsamplerate based resampler for the complicated cases */
124 if (resample_method
== PA_RESAMPLER_TRIVIAL
)
125 r
->resample_method
= PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
;
127 if (libsamplerate_init(r
) < 0)
131 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
132 if (trivial_init(r
) < 0)
145 void pa_resampler_free(pa_resampler
*r
) {
154 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
158 if (r
->i_ss
.rate
== rate
)
163 if (r
->impl_update_input_rate
)
164 r
->impl_update_input_rate(r
, rate
);
167 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
168 assert(r
&& in
&& out
&& r
->impl_run
);
170 r
->impl_run(r
, in
, out
);
173 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
176 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
179 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
181 return r
->resample_method
;
184 static const char * const resample_methods
[] = {
185 "src-sinc-best-quality",
186 "src-sinc-medium-quality",
188 "src-zero-order-hold",
193 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
195 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
198 return resample_methods
[m
];
201 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
202 pa_resample_method_t m
;
206 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
207 if (!strcmp(string
, resample_methods
[m
]))
210 return PA_RESAMPLER_INVALID
;
214 /*** libsamplerate based implementation ***/
216 static void libsamplerate_free(pa_resampler
*r
) {
217 struct impl_libsamplerate
*u
;
220 assert(r
->impl_data
);
225 src_delete(u
->src_state
);
228 pa_memblock_unref(u
->buf1_block
);
230 pa_memblock_unref(u
->buf2_block
);
232 pa_memblock_unref(u
->buf3_block
);
234 pa_memblock_unref(u
->buf4_block
);
238 static void calc_map_table(pa_resampler
*r
) {
239 struct impl_libsamplerate
*u
;
242 assert(r
->impl_data
);
246 if (!(u
->map_required
= (!pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) || r
->i_ss
.channels
!= r
->o_ss
.channels
)))
249 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
252 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
253 pa_channel_position_t a
, b
;
259 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
260 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
261 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
262 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
264 u
->map_table
[oc
][i
++] = ic
;
267 /* Add an end marker */
268 if (i
< PA_CHANNELS_MAX
)
269 u
->map_table
[oc
][i
] = -1;
273 static float * convert_to_float(pa_resampler
*r
, void *input
, unsigned n_frames
) {
274 struct impl_libsamplerate
*u
;
279 assert(r
->impl_data
);
282 /* Convert the incoming sample into floats and place them in buf1 */
284 if (!u
->to_float32ne_func
)
287 n_samples
= n_frames
* r
->i_ss
.channels
;
289 if (u
->buf1_samples
< n_samples
) {
291 pa_memblock_unref(u
->buf1_block
);
293 u
->buf1_samples
= n_samples
;
294 u
->buf1_block
= pa_memblock_new(r
->mempool
, sizeof(float) * n_samples
);
295 u
->buf1
= u
->buf1_block
->data
;
298 u
->to_float32ne_func(n_samples
, input
, u
->buf1
);
303 static float *remap_channels(pa_resampler
*r
, float *input
, unsigned n_frames
) {
304 struct impl_libsamplerate
*u
;
311 assert(r
->impl_data
);
314 /* Remap channels and place the result int buf2 */
316 if (!u
->map_required
)
319 n_samples
= n_frames
* r
->o_ss
.channels
;
321 if (u
->buf2_samples
< n_samples
) {
323 pa_memblock_unref(u
->buf2_block
);
325 u
->buf2_samples
= n_samples
;
326 u
->buf2_block
= pa_memblock_new(r
->mempool
, sizeof(float) * n_samples
);
327 u
->buf2
= u
->buf2_block
->data
;
330 memset(u
->buf2
, 0, n_samples
* sizeof(float));
332 o_skip
= sizeof(float) * r
->o_ss
.channels
;
333 i_skip
= sizeof(float) * r
->i_ss
.channels
;
335 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
337 static const float one
= 1.0;
339 for (i
= 0; i
< PA_CHANNELS_MAX
&& u
->map_table
[oc
][i
] >= 0; i
++)
341 u
->buf2
+ oc
, o_skip
,
342 u
->buf2
+ oc
, o_skip
,
343 input
+ u
->map_table
[oc
][i
], i_skip
,
351 static float *resample(pa_resampler
*r
, float *input
, unsigned *n_frames
) {
352 struct impl_libsamplerate
*u
;
354 unsigned out_n_frames
, out_n_samples
;
360 assert(r
->impl_data
);
363 /* Resample the data and place the result in buf3 */
368 out_n_frames
= (*n_frames
*r
->o_ss
.rate
/r
->i_ss
.rate
)+1024;
369 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
371 if (u
->buf3_samples
< out_n_samples
) {
373 pa_memblock_unref(u
->buf3_block
);
375 u
->buf3_samples
= out_n_samples
;
376 u
->buf3_block
= pa_memblock_new(r
->mempool
, sizeof(float) * out_n_samples
);
377 u
->buf3
= u
->buf3_block
->data
;
380 data
.data_in
= input
;
381 data
.input_frames
= *n_frames
;
383 data
.data_out
= u
->buf3
;
384 data
.output_frames
= out_n_frames
;
386 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
387 data
.end_of_input
= 0;
389 ret
= src_process(u
->src_state
, &data
);
391 assert((unsigned) data
.input_frames_used
== *n_frames
);
393 *n_frames
= data
.output_frames_gen
;
398 static void *convert_from_float(pa_resampler
*r
, float *input
, unsigned n_frames
) {
399 struct impl_libsamplerate
*u
;
404 assert(r
->impl_data
);
407 /* Convert the data into the correct sample type and place the result in buf4 */
409 if (!u
->from_float32ne_func
)
412 n_samples
= n_frames
* r
->o_ss
.channels
;
414 if (u
->buf4_samples
< n_samples
) {
416 pa_memblock_unref(u
->buf4_block
);
418 u
->buf4_samples
= n_samples
;
419 u
->buf4_block
= pa_memblock_new(r
->mempool
, sizeof(float) * n_samples
);
420 u
->buf4
= u
->buf4_block
->data
;
423 u
->from_float32ne_func(n_samples
, input
, u
->buf4
);
428 static void libsamplerate_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
429 struct impl_libsamplerate
*u
;
431 void *input
, *output
;
438 assert(in
->memblock
);
439 assert(in
->length
% r
->i_fz
== 0);
440 assert(r
->impl_data
);
444 input
= ((uint8_t*) in
->memblock
->data
+ in
->index
);
445 n_frames
= in
->length
/ r
->i_fz
;
446 assert(n_frames
> 0);
448 buf
= convert_to_float(r
, input
, n_frames
);
449 buf
= remap_channels(r
, buf
, n_frames
);
450 buf
= resample(r
, buf
, &n_frames
);
453 output
= convert_from_float(r
, buf
, n_frames
);
455 if (output
== input
) {
456 /* Mm, no adjustment has been necessary, so let's return the original block */
457 out
->memblock
= pa_memblock_ref(in
->memblock
);
458 out
->index
= in
->index
;
459 out
->length
= in
->length
;
461 out
->length
= n_frames
* r
->o_fz
;
463 out
->memblock
= NULL
;
465 if (output
== u
->buf1
) {
468 out
->memblock
= u
->buf1_block
;
469 u
->buf1_block
= NULL
;
470 } else if (output
== u
->buf2
) {
473 out
->memblock
= u
->buf2_block
;
474 u
->buf2_block
= NULL
;
475 } else if (output
== u
->buf3
) {
478 out
->memblock
= u
->buf3_block
;
479 u
->buf3_block
= NULL
;
480 } else if (output
== u
->buf4
) {
483 out
->memblock
= u
->buf4_block
;
484 u
->buf4_block
= NULL
;
487 assert(out
->memblock
);
490 out
->memblock
= NULL
;
491 out
->index
= out
->length
= 0;
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 u
->buf1
= u
->buf2
= u
->buf3
= u
->buf4
= NULL
;
520 u
->buf1_block
= u
->buf2_block
= u
->buf3_block
= u
->buf4_block
= NULL
;
521 u
->buf1_samples
= u
->buf2_samples
= u
->buf3_samples
= u
->buf4_samples
= 0;
523 if (r
->i_ss
.format
== PA_SAMPLE_FLOAT32NE
)
524 u
->to_float32ne_func
= NULL
;
525 else if (!(u
->to_float32ne_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
528 if (r
->o_ss
.format
== PA_SAMPLE_FLOAT32NE
)
529 u
->from_float32ne_func
= NULL
;
530 else if (!(u
->from_float32ne_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
533 if (r
->o_ss
.rate
== r
->i_ss
.rate
)
535 else if (!(u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
538 r
->impl_free
= libsamplerate_free
;
539 r
->impl_update_input_rate
= libsamplerate_update_input_rate
;
540 r
->impl_run
= libsamplerate_run
;
551 /* Trivial implementation */
553 static void trivial_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
556 struct impl_trivial
*u
;
561 assert(r
->impl_data
);
566 assert(fz
== r
->o_fz
);
568 n_frames
= in
->length
/fz
;
570 if (r
->i_ss
.rate
== r
->o_ss
.rate
) {
572 /* In case there's no diefference in sample types, do nothing */
574 pa_memblock_ref(out
->memblock
);
576 u
->o_counter
+= n_frames
;
578 /* Do real resampling */
582 /* The length of the new memory block rounded up */
583 l
= ((((n_frames
+1) * r
->o_ss
.rate
) / r
->i_ss
.rate
) + 1) * fz
;
586 out
->memblock
= pa_memblock_new(r
->mempool
, l
);
588 for (o_index
= 0;; o_index
++, u
->o_counter
++) {
591 j
= (u
->o_counter
* r
->i_ss
.rate
/ r
->o_ss
.rate
);
592 j
= j
> u
->i_counter
? j
- u
->i_counter
: 0;
597 assert(o_index
*fz
< out
->memblock
->length
);
599 memcpy((uint8_t*) out
->memblock
->data
+ fz
*o_index
,
600 (uint8_t*) in
->memblock
->data
+ in
->index
+ fz
*j
, fz
);
604 out
->length
= o_index
*fz
;
607 u
->i_counter
+= n_frames
;
609 /* Normalize counters */
610 while (u
->i_counter
>= r
->i_ss
.rate
) {
611 u
->i_counter
-= r
->i_ss
.rate
;
612 assert(u
->o_counter
>= r
->o_ss
.rate
);
613 u
->o_counter
-= r
->o_ss
.rate
;
617 static void trivial_free(pa_resampler
*r
) {
620 pa_xfree(r
->impl_data
);
623 static void trivial_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
624 struct impl_trivial
*u
;
628 assert(r
->impl_data
);
635 static int trivial_init(pa_resampler
*r
) {
636 struct impl_trivial
*u
;
639 assert(r
->i_ss
.format
== r
->o_ss
.format
);
640 assert(r
->i_ss
.channels
== r
->o_ss
.channels
);
642 r
->impl_data
= u
= pa_xnew(struct impl_trivial
, 1);
643 u
->o_counter
= u
->i_counter
= 0;
645 r
->impl_run
= trivial_run
;
646 r
->impl_free
= trivial_free
;
647 r
->impl_update_input_rate
= trivial_update_input_rate
;