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_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 assert(pa_sample_spec_valid(a));
89 assert(pa_sample_spec_valid(b));
90 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_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
169 assert(r && in && out && r->impl_run);
171 r->impl_run(r, in, out);
174 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
177 return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
180 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
182 return r->resample_method;
185 static const char * const resample_methods[] = {
186 "src-sinc-best-quality",
187 "src-sinc-medium-quality",
189 "src-zero-order-hold",
194 const char *pa_resample_method_to_string(pa_resample_method_t m) {
196 if (m < 0 || m >= PA_RESAMPLER_MAX)
199 return resample_methods[m];
202 pa_resample_method_t pa_parse_resample_method(const char *string) {
203 pa_resample_method_t m;
207 for (m = 0; m < PA_RESAMPLER_MAX; m++)
208 if (!strcmp(string, resample_methods[m]))
211 return PA_RESAMPLER_INVALID;
215 /*** libsamplerate based implementation ***/
217 static void libsamplerate_free(pa_resampler *r) {
218 struct impl_libsamplerate *u;
221 assert(r->impl_data);
226 src_delete(u->src_state);
228 if (u->buf1.memblock)
229 pa_memblock_unref(u->buf1.memblock);
230 if (u->buf2.memblock)
231 pa_memblock_unref(u->buf2.memblock);
232 if (u->buf3.memblock)
233 pa_memblock_unref(u->buf3.memblock);
234 if (u->buf4.memblock)
235 pa_memblock_unref(u->buf4.memblock);
239 static void calc_map_table(pa_resampler *r) {
240 struct impl_libsamplerate *u;
243 assert(r->impl_data);
247 if (!(u->map_required = (!pa_channel_map_equal(&r->i_cm, &r->o_cm) || r->i_ss.channels != r->o_ss.channels)))
250 for (oc = 0; oc < r->o_ss.channels; oc++) {
253 for (ic = 0; ic < r->i_ss.channels; ic++) {
254 pa_channel_position_t a, b;
260 (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_LEFT) ||
261 (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_RIGHT) ||
262 (a == PA_CHANNEL_POSITION_LEFT && b == PA_CHANNEL_POSITION_MONO) ||
263 (a == PA_CHANNEL_POSITION_RIGHT && b == PA_CHANNEL_POSITION_MONO))
265 u->map_table[oc][i++] = ic;
268 /* Add an end marker */
269 if (i < PA_CHANNELS_MAX)
270 u->map_table[oc][i] = -1;
274 static pa_memchunk* convert_to_float(pa_resampler *r, pa_memchunk *input) {
275 struct impl_libsamplerate *u;
281 assert(input->memblock);
283 assert(r->impl_data);
286 /* Convert the incoming sample into floats and place them in buf1 */
288 if (!u->to_float32ne_func || !input->length)
291 n_samples = (input->length / r->i_fz) * r->i_ss.channels;
293 if (!u->buf1.memblock || u->buf1_samples < n_samples) {
294 if (u->buf1.memblock)
295 pa_memblock_unref(u->buf1.memblock);
297 u->buf1_samples = n_samples;
298 u->buf1.memblock = pa_memblock_new(r->mempool, u->buf1.length = sizeof(float) * n_samples);
302 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
303 dst = (uint8_t*) pa_memblock_acquire(u->buf1.memblock);
304 u->to_float32ne_func(n_samples, src, dst);
305 pa_memblock_release(input->memblock);
306 pa_memblock_release(u->buf1.memblock);
308 u->buf1.length = sizeof(float) * n_samples;
313 static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
314 struct impl_libsamplerate *u;
315 unsigned n_samples, n_frames;
322 assert(input->memblock);
324 assert(r->impl_data);
327 /* Remap channels and place the result int buf2 */
329 if (!u->map_required || !input->length)
332 n_samples = input->length / sizeof(float);
333 n_frames = n_samples / r->o_ss.channels;
335 if (!u->buf2.memblock || u->buf2_samples < n_samples) {
336 if (u->buf2.memblock)
337 pa_memblock_unref(u->buf2.memblock);
339 u->buf2_samples = n_samples;
340 u->buf2.memblock = pa_memblock_new(r->mempool, u->buf2.length = sizeof(float) * n_samples);
344 src = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
345 dst = (float*) pa_memblock_acquire(u->buf2.memblock);
347 memset(dst, 0, n_samples * sizeof(float));
349 o_skip = sizeof(float) * r->o_ss.channels;
350 i_skip = sizeof(float) * r->i_ss.channels;
352 for (oc = 0; oc < r->o_ss.channels; oc++) {
354 static const float one = 1.0;
356 for (i = 0; i < PA_CHANNELS_MAX && u->map_table[oc][i] >= 0; i++)
360 src + u->map_table[oc][i], i_skip,
365 pa_memblock_release(input->memblock);
366 pa_memblock_release(u->buf2.memblock);
368 u->buf2.length = n_frames * sizeof(float) * r->o_ss.channels;
373 static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
374 struct impl_libsamplerate *u;
376 unsigned in_n_frames, in_n_samples;
377 unsigned out_n_frames, out_n_samples;
382 assert(r->impl_data);
385 /* Resample the data and place the result in buf3 */
387 if (!u->src_state || !input->length)
390 in_n_samples = input->length / sizeof(float);
391 in_n_frames = in_n_samples * r->o_ss.channels;
393 out_n_frames = (in_n_frames*r->o_ss.rate/r->i_ss.rate)+1024;
394 out_n_samples = out_n_frames * r->o_ss.channels;
396 if (!u->buf3.memblock || u->buf3_samples < out_n_samples) {
397 if (u->buf3.memblock)
398 pa_memblock_unref(u->buf3.memblock);
400 u->buf3_samples = out_n_samples;
401 u->buf3.memblock = pa_memblock_new(r->mempool, u->buf3.length = sizeof(float) * out_n_samples);
405 data.data_in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
406 data.input_frames = in_n_frames;
408 data.data_out = (float*) pa_memblock_acquire(u->buf3.memblock);
409 data.output_frames = out_n_frames;
411 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
412 data.end_of_input = 0;
414 ret = src_process(u->src_state, &data);
416 assert((unsigned) data.input_frames_used == in_n_frames);
418 pa_memblock_release(input->memblock);
419 pa_memblock_release(u->buf3.memblock);
421 u->buf3.length = data.output_frames_gen * sizeof(float) * r->o_ss.channels;
426 static pa_memchunk *convert_from_float(pa_resampler *r, pa_memchunk *input) {
427 struct impl_libsamplerate *u;
428 unsigned n_samples, n_frames;
433 assert(r->impl_data);
436 /* Convert the data into the correct sample type and place the result in buf4 */
438 if (!u->from_float32ne_func || !input->length)
441 n_frames = input->length / sizeof(float) / r->o_ss.channels;
442 n_samples = n_frames * r->o_ss.channels;
444 if (u->buf4_samples < n_samples) {
445 if (u->buf4.memblock)
446 pa_memblock_unref(u->buf4.memblock);
448 u->buf4_samples = n_samples;
449 u->buf4.memblock = pa_memblock_new(r->mempool, u->buf4.length = r->o_fz * n_frames);
453 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->length;
454 dst = pa_memblock_acquire(u->buf4.memblock);
455 u->from_float32ne_func(n_samples, src, dst);
456 pa_memblock_release(input->memblock);
457 pa_memblock_release(u->buf4.memblock);
459 u->buf4.length = r->o_fz * n_frames;
464 static void libsamplerate_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
465 struct impl_libsamplerate *u;
472 assert(in->memblock);
473 assert(in->length % r->i_fz == 0);
474 assert(r->impl_data);
478 buf = convert_to_float(r, (pa_memchunk*) in);
479 buf = remap_channels(r, buf);
480 buf = resample(r, buf);
483 buf = convert_from_float(r, buf);
487 pa_memblock_ref(buf->memblock);
489 pa_memchunk_reset(buf);
491 pa_memchunk_reset(out);
493 pa_memblock_release(in->memblock);
497 static void libsamplerate_update_input_rate(pa_resampler *r, uint32_t rate) {
498 struct impl_libsamplerate *u;
502 assert(r->impl_data);
507 u->src_state = src_new(r->resample_method, r->o_ss.channels, &err);
508 assert(u->src_state);
510 int ret = src_set_ratio(u->src_state, (double) r->o_ss.rate / rate);
515 static int libsamplerate_init(pa_resampler *r) {
516 struct impl_libsamplerate *u = NULL;
519 r->impl_data = u = pa_xnew(struct impl_libsamplerate, 1);
521 pa_memchunk_reset(&u->buf1);
522 pa_memchunk_reset(&u->buf2);
523 pa_memchunk_reset(&u->buf3);
524 pa_memchunk_reset(&u->buf4);
525 u->buf1_samples = u->buf2_samples = u->buf3_samples = u->buf4_samples = 0;
527 if (r->i_ss.format == PA_SAMPLE_FLOAT32NE)
528 u->to_float32ne_func = NULL;
529 else if (!(u->to_float32ne_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
532 if (r->o_ss.format == PA_SAMPLE_FLOAT32NE)
533 u->from_float32ne_func = NULL;
534 else if (!(u->from_float32ne_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
537 if (r->o_ss.rate == r->i_ss.rate)
539 else if (!(u->src_state = src_new(r->resample_method, r->o_ss.channels, &err)))
542 r->impl_free = libsamplerate_free;
543 r->impl_update_input_rate = libsamplerate_update_input_rate;
544 r->impl_run = libsamplerate_run;
555 /* Trivial implementation */
557 static void trivial_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
560 struct impl_trivial *u;
565 assert(r->impl_data);
570 assert(fz == r->o_fz);
572 n_frames = in->length/fz;
574 if (r->i_ss.rate == r->o_ss.rate) {
576 /* In case there's no diefference in sample types, do nothing */
578 pa_memblock_ref(out->memblock);
580 u->o_counter += n_frames;
582 /* Do real resampling */
587 /* The length of the new memory block rounded up */
588 l = ((((n_frames+1) * r->o_ss.rate) / r->i_ss.rate) + 1) * fz;
591 out->memblock = pa_memblock_new(r->mempool, l);
593 src = (uint8_t*) pa_memblock_acquire(in->memblock) + in->index;
594 dst = pa_memblock_acquire(out->memblock);
596 for (o_index = 0;; o_index++, u->o_counter++) {
599 j = (u->o_counter * r->i_ss.rate / r->o_ss.rate);
600 j = j > u->i_counter ? j - u->i_counter : 0;
605 assert(o_index*fz < pa_memblock_get_length(out->memblock));
607 memcpy((uint8_t*) dst + fz*o_index,
608 (uint8_t*) src + fz*j, fz);
612 pa_memblock_release(in->memblock);
613 pa_memblock_release(out->memblock);
615 out->length = o_index*fz;
618 u->i_counter += n_frames;
620 /* Normalize counters */
621 while (u->i_counter >= r->i_ss.rate) {
622 u->i_counter -= r->i_ss.rate;
623 assert(u->o_counter >= r->o_ss.rate);
624 u->o_counter -= r->o_ss.rate;
628 static void trivial_free(pa_resampler *r) {
631 pa_xfree(r->impl_data);
634 static void trivial_update_input_rate(pa_resampler *r, uint32_t rate) {
635 struct impl_trivial *u;
639 assert(r->impl_data);
646 static int trivial_init(pa_resampler*r) {
647 struct impl_trivial *u;
650 assert(r->i_ss.format == r->o_ss.format);
651 assert(r->i_ss.channels == r->o_ss.channels);
653 r->impl_data = u = pa_xnew(struct impl_trivial, 1);
654 u->o_counter = u->i_counter = 0;
656 r->impl_run = trivial_run;
657 r->impl_free = trivial_free;
658 r->impl_update_input_rate = trivial_update_input_rate;