4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; 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 "resampler.h"
39 pa_resample_method_t resample_method
;
40 pa_sample_spec i_ss
, o_ss
;
41 pa_channel_map i_cm
, o_cm
;
43 pa_memblock_stat
*memblock_stat
;
45 void (*impl_free
)(pa_resampler
*r
);
46 void (*impl_update_input_rate
)(pa_resampler
*r
, uint32_t rate
);
47 void (*impl_run
)(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
);
51 struct impl_libsamplerate
{
52 float* buf1
, *buf2
, *buf3
, *buf4
;
53 unsigned buf1_samples
, buf2_samples
, buf3_samples
, buf4_samples
;
55 pa_convert_to_float32ne_func_t to_float32ne_func
;
56 pa_convert_from_float32ne_func_t from_float32ne_func
;
59 int map_table
[PA_CHANNELS_MAX
][PA_CHANNELS_MAX
];
68 static int libsamplerate_init(pa_resampler
*r
);
69 static int trivial_init(pa_resampler
*r
);
71 pa_resampler
* pa_resampler_new(
72 const pa_sample_spec
*a
,
73 const pa_channel_map
*am
,
74 const pa_sample_spec
*b
,
75 const pa_channel_map
*bm
,
77 pa_resample_method_t resample_method
) {
79 pa_resampler
*r
= NULL
;
83 assert(pa_sample_spec_valid(a
));
84 assert(pa_sample_spec_valid(b
));
85 assert(resample_method
!= PA_RESAMPLER_INVALID
);
87 r
= pa_xnew(pa_resampler
, 1);
90 r
->resample_method
= resample_method
;
93 r
->impl_update_input_rate
= NULL
;
96 /* Fill sample specs */
103 pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
);
108 pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
);
110 r
->i_fz
= pa_frame_size(a
);
111 r
->o_fz
= pa_frame_size(b
);
113 /* Choose implementation */
114 if (a
->channels
!= b
->channels
||
115 a
->format
!= b
->format
||
116 !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) ||
117 resample_method
!= PA_RESAMPLER_TRIVIAL
) {
119 /* Use the libsamplerate based resampler for the complicated cases */
120 if (resample_method
== PA_RESAMPLER_TRIVIAL
)
121 r
->resample_method
= PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
;
123 if (libsamplerate_init(r
) < 0)
127 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
128 if (trivial_init(r
) < 0)
141 void pa_resampler_free(pa_resampler
*r
) {
150 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
154 if (r
->i_ss
.rate
== rate
)
159 if (r
->impl_update_input_rate
)
160 r
->impl_update_input_rate(r
, rate
);
163 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
164 assert(r
&& in
&& out
&& r
->impl_run
);
166 r
->impl_run(r
, in
, out
);
169 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
170 assert(r
&& (out_length
% r
->o_fz
) == 0);
171 return (((out_length
/ r
->o_fz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_fz
;
174 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
176 return r
->resample_method
;
179 static const char * const resample_methods
[] = {
180 "src-sinc-best-quality",
181 "src-sinc-medium-quality",
183 "src-zero-order-hold",
188 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
190 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
193 return resample_methods
[m
];
196 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
197 pa_resample_method_t m
;
201 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
202 if (!strcmp(string
, resample_methods
[m
]))
205 return PA_RESAMPLER_INVALID
;
209 /*** libsamplerate based implementation ***/
211 static void libsamplerate_free(pa_resampler
*r
) {
212 struct impl_libsamplerate
*u
;
215 assert(r
->impl_data
);
220 src_delete(u
->src_state
);
229 static void calc_map_table(pa_resampler
*r
) {
230 struct impl_libsamplerate
*u
;
233 assert(r
->impl_data
);
237 if (!(u
->map_required
= (!pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
) || r
->i_ss
.channels
!= r
->o_ss
.channels
)))
240 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
243 for (ic
= 0; ic
< r
->i_ss
.channels
; ic
++) {
244 pa_channel_position_t a
, b
;
250 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_LEFT
) ||
251 (a
== PA_CHANNEL_POSITION_MONO
&& b
== PA_CHANNEL_POSITION_RIGHT
) ||
252 (a
== PA_CHANNEL_POSITION_LEFT
&& b
== PA_CHANNEL_POSITION_MONO
) ||
253 (a
== PA_CHANNEL_POSITION_RIGHT
&& b
== PA_CHANNEL_POSITION_MONO
))
255 u
->map_table
[oc
][i
++] = ic
;
258 /* Add an end marker */
259 if (i
< PA_CHANNELS_MAX
)
260 u
->map_table
[oc
][i
] = -1;
264 static float * convert_to_float(pa_resampler
*r
, float *input
, unsigned n_frames
) {
265 struct impl_libsamplerate
*u
;
270 assert(r
->impl_data
);
273 /* Convert the incoming sample into floats and place them in buf1 */
275 if (!u
->to_float32ne_func
)
278 n_samples
= n_frames
* r
->i_ss
.channels
;
280 if (u
->buf1_samples
< n_samples
)
281 u
->buf1
= pa_xrealloc(u
->buf1
, sizeof(float) * (u
->buf1_samples
= n_samples
));
283 u
->to_float32ne_func(n_samples
, input
, u
->buf1
);
288 static float *remap_channels(pa_resampler
*r
, float *input
, unsigned n_frames
) {
289 struct impl_libsamplerate
*u
;
296 assert(r
->impl_data
);
299 /* Remap channels and place the result int buf2 */
301 if (!u
->map_required
)
304 n_samples
= n_frames
* r
->o_ss
.channels
;
306 if (u
->buf2_samples
< n_samples
)
307 u
->buf2
= pa_xrealloc(u
->buf2
, sizeof(float) * (u
->buf2_samples
= n_samples
));
309 memset(u
->buf2
, 0, n_samples
* sizeof(float));
311 o_skip
= sizeof(float) * r
->o_ss
.channels
;
312 i_skip
= sizeof(float) * r
->i_ss
.channels
;
314 for (oc
= 0; oc
< r
->o_ss
.channels
; oc
++) {
316 static const float one
= 1.0;
318 for (i
= 0; i
< PA_CHANNELS_MAX
&& u
->map_table
[oc
][i
] >= 0; i
++)
320 u
->buf2
+ oc
, o_skip
,
321 u
->buf2
+ oc
, o_skip
,
322 input
+ u
->map_table
[oc
][i
], i_skip
,
330 static float *resample(pa_resampler
*r
, float *input
, unsigned *n_frames
) {
331 struct impl_libsamplerate
*u
;
333 unsigned out_n_frames
, out_n_samples
;
339 assert(r
->impl_data
);
342 /* Resample the data and place the result in buf3 */
347 out_n_frames
= (*n_frames
*r
->o_ss
.rate
/r
->i_ss
.rate
)+1024;
348 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
350 if (u
->buf3_samples
< out_n_samples
)
351 u
->buf3
= pa_xrealloc(u
->buf3
, sizeof(float) * (u
->buf3_samples
= out_n_samples
));
353 data
.data_in
= input
;
354 data
.input_frames
= *n_frames
;
356 data
.data_out
= u
->buf3
;
357 data
.output_frames
= out_n_frames
;
359 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
360 data
.end_of_input
= 0;
362 ret
= src_process(u
->src_state
, &data
);
364 assert((unsigned) data
.input_frames_used
== *n_frames
);
366 *n_frames
= data
.output_frames_gen
;
371 static float *convert_from_float(pa_resampler
*r
, float *input
, unsigned n_frames
) {
372 struct impl_libsamplerate
*u
;
377 assert(r
->impl_data
);
380 /* Convert the data into the correct sample type and place the result in buf4 */
382 if (!u
->from_float32ne_func
)
385 n_samples
= n_frames
* r
->o_ss
.channels
;
387 if (u
->buf4_samples
< n_samples
)
388 u
->buf4
= pa_xrealloc(u
->buf4
, sizeof(float) * (u
->buf4_samples
= n_samples
));
390 u
->from_float32ne_func(n_samples
, input
, u
->buf4
);
395 static void libsamplerate_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
396 struct impl_libsamplerate
*u
;
404 assert(in
->memblock
);
405 assert(in
->length
% r
->i_fz
== 0);
406 assert(r
->impl_data
);
410 buf
= input
= (float*) ((uint8_t*) in
->memblock
->data
+ in
->index
);
411 n_frames
= in
->length
/ r
->i_fz
;
412 assert(n_frames
> 0);
414 buf
= convert_to_float(r
, buf
, n_frames
);
415 buf
= remap_channels(r
, buf
, n_frames
);
416 buf
= resample(r
, buf
, &n_frames
);
419 buf
= convert_from_float(r
, buf
, n_frames
);
422 /* Mm, no adjustment has been necessary, so let's return the original block */
423 out
->memblock
= pa_memblock_ref(in
->memblock
);
424 out
->index
= in
->index
;
425 out
->length
= in
->length
;
429 out
->length
= n_frames
* r
->o_fz
;
432 if (buf
== u
->buf1
) {
435 } else if (buf
== u
->buf2
) {
438 } else if (buf
== u
->buf3
) {
441 } else if (buf
== u
->buf4
) {
448 /* Take the existing buffer and make it a memblock */
449 out
->memblock
= pa_memblock_new_dynamic(*p
, out
->length
, r
->memblock_stat
);
453 out
->memblock
= NULL
;
454 out
->index
= out
->length
= 0;
458 static void libsamplerate_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
459 struct impl_libsamplerate
*u
;
463 assert(r
->impl_data
);
468 u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
);
469 assert(u
->src_state
);
471 int ret
= src_set_ratio(u
->src_state
, (double) r
->o_ss
.rate
/ rate
);
476 static int libsamplerate_init(pa_resampler
*r
) {
477 struct impl_libsamplerate
*u
= NULL
;
480 r
->impl_data
= u
= pa_xnew(struct impl_libsamplerate
, 1);
482 u
->buf1
= u
->buf2
= u
->buf3
= u
->buf4
= NULL
;
483 u
->buf1_samples
= u
->buf2_samples
= u
->buf3_samples
= u
->buf4_samples
= 0;
485 if (r
->i_ss
.format
== PA_SAMPLE_FLOAT32NE
)
486 u
->to_float32ne_func
= NULL
;
487 else if (!(u
->to_float32ne_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
490 if (r
->o_ss
.format
== PA_SAMPLE_FLOAT32NE
)
491 u
->from_float32ne_func
= NULL
;
492 else if (!(u
->from_float32ne_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
495 if (r
->o_ss
.rate
== r
->i_ss
.rate
)
497 else if (!(u
->src_state
= src_new(r
->resample_method
, r
->o_ss
.channels
, &err
)))
500 r
->impl_free
= libsamplerate_free
;
501 r
->impl_update_input_rate
= libsamplerate_update_input_rate
;
502 r
->impl_run
= libsamplerate_run
;
513 /* Trivial implementation */
515 static void trivial_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
518 struct impl_trivial
*u
;
523 assert(r
->impl_data
);
528 assert(fz
== r
->o_fz
);
530 n_frames
= in
->length
/fz
;
532 if (r
->i_ss
.rate
== r
->o_ss
.rate
) {
534 /* In case there's no diefference in sample types, do nothing */
536 pa_memblock_ref(out
->memblock
);
538 u
->o_counter
+= n_frames
;
540 /* Do real resampling */
544 /* The length of the new memory block rounded up */
545 l
= ((((n_frames
+1) * r
->o_ss
.rate
) / r
->i_ss
.rate
) + 1) * fz
;
548 out
->memblock
= pa_memblock_new(l
, r
->memblock_stat
);
550 for (o_index
= 0;; o_index
++, u
->o_counter
++) {
553 j
= (u
->o_counter
* r
->i_ss
.rate
/ r
->o_ss
.rate
);
554 j
= j
> u
->i_counter
? j
- u
->i_counter
: 0;
559 assert(o_index
*fz
< out
->memblock
->length
);
561 memcpy((uint8_t*) out
->memblock
->data
+ fz
*o_index
,
562 (uint8_t*) in
->memblock
->data
+ in
->index
+ fz
*j
, fz
);
566 out
->length
= o_index
*fz
;
569 u
->i_counter
+= n_frames
;
571 /* Normalize counters */
572 while (u
->i_counter
>= r
->i_ss
.rate
) {
573 u
->i_counter
-= r
->i_ss
.rate
;
574 assert(u
->o_counter
>= r
->o_ss
.rate
);
575 u
->o_counter
-= r
->o_ss
.rate
;
579 static void trivial_free(pa_resampler
*r
) {
582 pa_xfree(r
->impl_data
);
585 static void trivial_update_input_rate(pa_resampler
*r
, uint32_t rate
) {
586 struct impl_trivial
*u
;
590 assert(r
->impl_data
);
597 static int trivial_init(pa_resampler
*r
) {
598 struct impl_trivial
*u
;
601 assert(r
->i_ss
.format
== r
->o_ss
.format
);
602 assert(r
->i_ss
.channels
== r
->o_ss
.channels
);
604 r
->impl_data
= u
= pa_xnew(struct impl_trivial
, 1);
605 u
->o_counter
= u
->i_counter
= 0;
607 r
->impl_run
= trivial_run
;
608 r
->impl_free
= trivial_free
;
609 r
->impl_update_input_rate
= trivial_update_input_rate
;