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_memblock *buf1_block, *buf2_block, *buf3_block, *buf4_block;
57 float* 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_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
170 assert(r && in && out && r->impl_run);
172 r->impl_run(r, in, out);
175 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
178 return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
181 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
183 return r->resample_method;
186 static const char * const resample_methods[] = {
187 "src-sinc-best-quality",
188 "src-sinc-medium-quality",
190 "src-zero-order-hold",
195 const char *pa_resample_method_to_string(pa_resample_method_t m) {
197 if (m < 0 || m >= PA_RESAMPLER_MAX)
200 return resample_methods[m];
203 pa_resample_method_t pa_parse_resample_method(const char *string) {
204 pa_resample_method_t m;
208 for (m = 0; m < PA_RESAMPLER_MAX; m++)
209 if (!strcmp(string, resample_methods[m]))
212 return PA_RESAMPLER_INVALID;
216 /*** libsamplerate based implementation ***/
218 static void libsamplerate_free(pa_resampler *r) {
219 struct impl_libsamplerate *u;
222 assert(r->impl_data);
227 src_delete(u->src_state);
230 pa_memblock_unref(u->buf1_block);
232 pa_memblock_unref(u->buf2_block);
234 pa_memblock_unref(u->buf3_block);
236 pa_memblock_unref(u->buf4_block);
240 static void calc_map_table(pa_resampler *r) {
241 struct impl_libsamplerate *u;
244 assert(r->impl_data);
248 if (!(u->map_required = (!pa_channel_map_equal(&r->i_cm, &r->o_cm) || r->i_ss.channels != r->o_ss.channels)))
251 for (oc = 0; oc < r->o_ss.channels; oc++) {
254 for (ic = 0; ic < r->i_ss.channels; ic++) {
255 pa_channel_position_t a, b;
261 (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_LEFT) ||
262 (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_RIGHT) ||
263 (a == PA_CHANNEL_POSITION_LEFT && b == PA_CHANNEL_POSITION_MONO) ||
264 (a == PA_CHANNEL_POSITION_RIGHT && b == PA_CHANNEL_POSITION_MONO))
266 u->map_table[oc][i++] = ic;
269 /* Add an end marker */
270 if (i < PA_CHANNELS_MAX)
271 u->map_table[oc][i] = -1;
275 static float * convert_to_float(pa_resampler *r, void *input, unsigned n_frames) {
276 struct impl_libsamplerate *u;
281 assert(r->impl_data);
284 /* Convert the incoming sample into floats and place them in buf1 */
286 if (!u->to_float32ne_func)
289 n_samples = n_frames * r->i_ss.channels;
291 if (u->buf1_samples < n_samples) {
293 pa_memblock_unref(u->buf1_block);
295 u->buf1_samples = n_samples;
296 u->buf1_block = pa_memblock_new(r->mempool, sizeof(float) * n_samples);
297 u->buf1 = u->buf1_block->data;
300 u->to_float32ne_func(n_samples, input, u->buf1);
305 static float *remap_channels(pa_resampler *r, float *input, unsigned n_frames) {
306 struct impl_libsamplerate *u;
313 assert(r->impl_data);
316 /* Remap channels and place the result int buf2 */
318 if (!u->map_required)
321 n_samples = n_frames * r->o_ss.channels;
323 if (u->buf2_samples < n_samples) {
325 pa_memblock_unref(u->buf2_block);
327 u->buf2_samples = n_samples;
328 u->buf2_block = pa_memblock_new(r->mempool, sizeof(float) * n_samples);
329 u->buf2 = u->buf2_block->data;
332 memset(u->buf2, 0, n_samples * sizeof(float));
334 o_skip = sizeof(float) * r->o_ss.channels;
335 i_skip = sizeof(float) * r->i_ss.channels;
337 for (oc = 0; oc < r->o_ss.channels; oc++) {
339 static const float one = 1.0;
341 for (i = 0; i < PA_CHANNELS_MAX && u->map_table[oc][i] >= 0; i++)
343 u->buf2 + oc, o_skip,
344 u->buf2 + oc, o_skip,
345 input + u->map_table[oc][i], i_skip,
353 static float *resample(pa_resampler *r, float *input, unsigned *n_frames) {
354 struct impl_libsamplerate *u;
356 unsigned out_n_frames, out_n_samples;
362 assert(r->impl_data);
365 /* Resample the data and place the result in buf3 */
370 out_n_frames = (*n_frames*r->o_ss.rate/r->i_ss.rate)+1024;
371 out_n_samples = out_n_frames * r->o_ss.channels;
373 if (u->buf3_samples < out_n_samples) {
375 pa_memblock_unref(u->buf3_block);
377 u->buf3_samples = out_n_samples;
378 u->buf3_block = pa_memblock_new(r->mempool, sizeof(float) * out_n_samples);
379 u->buf3 = u->buf3_block->data;
382 data.data_in = input;
383 data.input_frames = *n_frames;
385 data.data_out = u->buf3;
386 data.output_frames = out_n_frames;
388 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
389 data.end_of_input = 0;
391 ret = src_process(u->src_state, &data);
393 assert((unsigned) data.input_frames_used == *n_frames);
395 *n_frames = data.output_frames_gen;
400 static void *convert_from_float(pa_resampler *r, float *input, unsigned n_frames) {
401 struct impl_libsamplerate *u;
406 assert(r->impl_data);
409 /* Convert the data into the correct sample type and place the result in buf4 */
411 if (!u->from_float32ne_func)
414 n_samples = n_frames * r->o_ss.channels;
416 if (u->buf4_samples < n_samples) {
418 pa_memblock_unref(u->buf4_block);
420 u->buf4_samples = n_samples;
421 u->buf4_block = pa_memblock_new(r->mempool, sizeof(float) * n_samples);
422 u->buf4 = u->buf4_block->data;
425 u->from_float32ne_func(n_samples, input, u->buf4);
430 static void libsamplerate_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
431 struct impl_libsamplerate *u;
433 void *input, *output;
440 assert(in->memblock);
441 assert(in->length % r->i_fz == 0);
442 assert(r->impl_data);
446 input = ((uint8_t*) in->memblock->data + in->index);
447 n_frames = in->length / r->i_fz;
448 assert(n_frames > 0);
450 buf = convert_to_float(r, input, n_frames);
451 buf = remap_channels(r, buf, n_frames);
452 buf = resample(r, buf, &n_frames);
455 output = convert_from_float(r, buf, n_frames);
457 if (output == input) {
458 /* Mm, no adjustment has been necessary, so let's return the original block */
459 out->memblock = pa_memblock_ref(in->memblock);
460 out->index = in->index;
461 out->length = in->length;
463 out->length = n_frames * r->o_fz;
465 out->memblock = NULL;
467 if (output == u->buf1) {
470 out->memblock = u->buf1_block;
471 u->buf1_block = NULL;
472 } else if (output == u->buf2) {
475 out->memblock = u->buf2_block;
476 u->buf2_block = NULL;
477 } else if (output == u->buf3) {
480 out->memblock = u->buf3_block;
481 u->buf3_block = NULL;
482 } else if (output == u->buf4) {
485 out->memblock = u->buf4_block;
486 u->buf4_block = NULL;
489 assert(out->memblock);
492 out->memblock = NULL;
493 out->index = out->length = 0;
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 u->buf1 = u->buf2 = u->buf3 = u->buf4 = NULL;
522 u->buf1_block = u->buf2_block = u->buf3_block = u->buf4_block = NULL;
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 */
584 /* The length of the new memory block rounded up */
585 l = ((((n_frames+1) * r->o_ss.rate) / r->i_ss.rate) + 1) * fz;
588 out->memblock = pa_memblock_new(r->mempool, l);
590 for (o_index = 0;; o_index++, u->o_counter++) {
593 j = (u->o_counter * r->i_ss.rate / r->o_ss.rate);
594 j = j > u->i_counter ? j - u->i_counter : 0;
599 assert(o_index*fz < out->memblock->length);
601 memcpy((uint8_t*) out->memblock->data + fz*o_index,
602 (uint8_t*) in->memblock->data + in->index + fz*j, fz);
606 out->length = o_index*fz;
609 u->i_counter += n_frames;
611 /* Normalize counters */
612 while (u->i_counter >= r->i_ss.rate) {
613 u->i_counter -= r->i_ss.rate;
614 assert(u->o_counter >= r->o_ss.rate);
615 u->o_counter -= r->o_ss.rate;
619 static void trivial_free(pa_resampler *r) {
622 pa_xfree(r->impl_data);
625 static void trivial_update_input_rate(pa_resampler *r, uint32_t rate) {
626 struct impl_trivial *u;
630 assert(r->impl_data);
637 static int trivial_init(pa_resampler*r) {
638 struct impl_trivial *u;
641 assert(r->i_ss.format == r->o_ss.format);
642 assert(r->i_ss.channels == r->o_ss.channels);
644 r->impl_data = u = pa_xnew(struct impl_trivial, 1);
645 u->o_counter = u->i_counter = 0;
647 r->impl_run = trivial_run;
648 r->impl_free = trivial_free;
649 r->impl_update_input_rate = trivial_update_input_rate;