]> code.delx.au - pulseaudio/blob - src/polypcore/resampler.c
0417e44efe658c2c268cd7a62d6d8e4edf5fb051
[pulseaudio] / src / polypcore / resampler.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <string.h>
28
29 #include <samplerate.h>
30 #include <liboil/liboilfuncs.h>
31 #include <liboil/liboil.h>
32
33 #include "resampler.h"
34 #include "sconv.h"
35 #include "xmalloc.h"
36 #include "log.h"
37
38 struct pa_resampler {
39 pa_resample_method_t resample_method;
40 pa_sample_spec i_ss, o_ss;
41 pa_channel_map i_cm, o_cm;
42 size_t i_fz, o_fz;
43 pa_memblock_stat *memblock_stat;
44
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);
48 void *impl_data;
49 };
50
51 struct impl_libsamplerate {
52 float* buf1, *buf2, *buf3, *buf4;
53 unsigned buf1_samples, buf2_samples, buf3_samples, buf4_samples;
54
55 pa_convert_to_float32ne_func_t to_float32ne_func;
56 pa_convert_from_float32ne_func_t from_float32ne_func;
57 SRC_STATE *src_state;
58
59 int map_table[PA_CHANNELS_MAX][PA_CHANNELS_MAX];
60 int map_required;
61 };
62
63 struct impl_trivial {
64 unsigned o_counter;
65 unsigned i_counter;
66 };
67
68 static int libsamplerate_init(pa_resampler*r);
69 static int trivial_init(pa_resampler*r);
70
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,
76 pa_memblock_stat *s,
77 pa_resample_method_t resample_method) {
78
79 pa_resampler *r = NULL;
80
81 assert(a);
82 assert(b);
83 assert(pa_sample_spec_valid(a));
84 assert(pa_sample_spec_valid(b));
85 assert(resample_method != PA_RESAMPLER_INVALID);
86
87 r = pa_xnew(pa_resampler, 1);
88 r->impl_data = NULL;
89 r->memblock_stat = s;
90 r->resample_method = resample_method;
91
92 r->impl_free = NULL;
93 r->impl_update_input_rate = NULL;
94 r->impl_run = NULL;
95
96 /* Fill sample specs */
97 r->i_ss = *a;
98 r->o_ss = *b;
99
100 if (am)
101 r->i_cm = *am;
102 else
103 pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels);
104
105 if (bm)
106 r->o_cm = *bm;
107 else
108 pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels);
109
110 r->i_fz = pa_frame_size(a);
111 r->o_fz = pa_frame_size(b);
112
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) {
118
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;
122
123 if (libsamplerate_init(r) < 0)
124 goto fail;
125
126 } else {
127 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
128 if (trivial_init(r) < 0)
129 goto fail;
130 }
131
132 return r;
133
134 fail:
135 if (r)
136 pa_xfree(r);
137
138 return NULL;
139 }
140
141 void pa_resampler_free(pa_resampler *r) {
142 assert(r);
143
144 if (r->impl_free)
145 r->impl_free(r);
146
147 pa_xfree(r);
148 }
149
150 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
151 assert(r);
152 assert(rate > 0);
153
154 if (r->i_ss.rate == rate)
155 return;
156
157 r->i_ss.rate = rate;
158
159 if (r->impl_update_input_rate)
160 r->impl_update_input_rate(r, rate);
161 }
162
163 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
164 assert(r && in && out && r->impl_run);
165
166 r->impl_run(r, in, out);
167 }
168
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;
172 }
173
174 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
175 assert(r);
176 return r->resample_method;
177 }
178
179 static const char * const resample_methods[] = {
180 "src-sinc-best-quality",
181 "src-sinc-medium-quality",
182 "src-sinc-fastest",
183 "src-zero-order-hold",
184 "src-linear",
185 "trivial"
186 };
187
188 const char *pa_resample_method_to_string(pa_resample_method_t m) {
189
190 if (m < 0 || m >= PA_RESAMPLER_MAX)
191 return NULL;
192
193 return resample_methods[m];
194 }
195
196 pa_resample_method_t pa_parse_resample_method(const char *string) {
197 pa_resample_method_t m;
198
199 assert(string);
200
201 for (m = 0; m < PA_RESAMPLER_MAX; m++)
202 if (!strcmp(string, resample_methods[m]))
203 return m;
204
205 return PA_RESAMPLER_INVALID;
206 }
207
208
209 /*** libsamplerate based implementation ***/
210
211 static void libsamplerate_free(pa_resampler *r) {
212 struct impl_libsamplerate *u;
213
214 assert(r);
215 assert(r->impl_data);
216
217 u = r->impl_data;
218
219 if (u->src_state)
220 src_delete(u->src_state);
221
222 pa_xfree(u->buf1);
223 pa_xfree(u->buf2);
224 pa_xfree(u->buf3);
225 pa_xfree(u->buf4);
226 pa_xfree(u);
227 }
228
229 static void calc_map_table(pa_resampler *r) {
230 struct impl_libsamplerate *u;
231 unsigned oc;
232 assert(r);
233 assert(r->impl_data);
234
235 u = r->impl_data;
236
237 if (!(u->map_required = (!pa_channel_map_equal(&r->i_cm, &r->o_cm) || r->i_ss.channels != r->o_ss.channels)))
238 return;
239
240 for (oc = 0; oc < r->o_ss.channels; oc++) {
241 unsigned ic, i = 0;
242
243 for (ic = 0; ic < r->i_ss.channels; ic++) {
244 pa_channel_position_t a, b;
245
246 a = r->i_cm.map[ic];
247 b = r->o_cm.map[oc];
248
249 if (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))
254
255 u->map_table[oc][i++] = ic;
256 }
257
258 /* Add an end marker */
259 if (i < PA_CHANNELS_MAX)
260 u->map_table[oc][i] = -1;
261 }
262 }
263
264 static float * convert_to_float(pa_resampler *r, float *input, unsigned n_frames) {
265 struct impl_libsamplerate *u;
266 unsigned n_samples;
267
268 assert(r);
269 assert(input);
270 assert(r->impl_data);
271 u = r->impl_data;
272
273 /* Convert the incoming sample into floats and place them in buf1 */
274
275 if (!u->to_float32ne_func)
276 return input;
277
278 n_samples = n_frames * r->i_ss.channels;
279
280 if (u->buf1_samples < n_samples)
281 u->buf1 = pa_xrealloc(u->buf1, sizeof(float) * (u->buf1_samples = n_samples));
282
283 u->to_float32ne_func(n_samples, input, u->buf1);
284
285 return u->buf1;
286 }
287
288 static float *remap_channels(pa_resampler *r, float *input, unsigned n_frames) {
289 struct impl_libsamplerate *u;
290 unsigned n_samples;
291 int i_skip, o_skip;
292 unsigned oc;
293
294 assert(r);
295 assert(input);
296 assert(r->impl_data);
297 u = r->impl_data;
298
299 /* Remap channels and place the result int buf2 */
300
301 if (!u->map_required)
302 return input;
303
304 n_samples = n_frames * r->o_ss.channels;
305
306 if (u->buf2_samples < n_samples)
307 u->buf2 = pa_xrealloc(u->buf2, sizeof(float) * (u->buf2_samples = n_samples));
308
309 memset(u->buf2, 0, n_samples * sizeof(float));
310
311 o_skip = sizeof(float) * r->o_ss.channels;
312 i_skip = sizeof(float) * r->i_ss.channels;
313
314 for (oc = 0; oc < r->o_ss.channels; oc++) {
315 unsigned i;
316 static const float one = 1.0;
317
318 for (i = 0; i < PA_CHANNELS_MAX && u->map_table[oc][i] >= 0; i++)
319 oil_vectoradd_f32(
320 u->buf2 + oc, o_skip,
321 u->buf2 + oc, o_skip,
322 input + u->map_table[oc][i], i_skip,
323 n_frames,
324 &one, &one);
325 }
326
327 return u->buf2;
328 }
329
330 static float *resample(pa_resampler *r, float *input, unsigned *n_frames) {
331 struct impl_libsamplerate *u;
332 SRC_DATA data;
333 unsigned out_n_frames, out_n_samples;
334 int ret;
335
336 assert(r);
337 assert(input);
338 assert(n_frames);
339 assert(r->impl_data);
340 u = r->impl_data;
341
342 /* Resample the data and place the result in buf3 */
343
344 if (!u->src_state)
345 return input;
346
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;
349
350 if (u->buf3_samples < out_n_samples)
351 u->buf3 = pa_xrealloc(u->buf3, sizeof(float) * (u->buf3_samples = out_n_samples));
352
353 data.data_in = input;
354 data.input_frames = *n_frames;
355
356 data.data_out = u->buf3;
357 data.output_frames = out_n_frames;
358
359 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
360 data.end_of_input = 0;
361
362 ret = src_process(u->src_state, &data);
363 assert(ret == 0);
364 assert((unsigned) data.input_frames_used == *n_frames);
365
366 *n_frames = data.output_frames_gen;
367
368 return u->buf3;
369 }
370
371 static float *convert_from_float(pa_resampler *r, float *input, unsigned n_frames) {
372 struct impl_libsamplerate *u;
373 unsigned n_samples;
374
375 assert(r);
376 assert(input);
377 assert(r->impl_data);
378 u = r->impl_data;
379
380 /* Convert the data into the correct sample type and place the result in buf4 */
381
382 if (!u->from_float32ne_func)
383 return input;
384
385 n_samples = n_frames * r->o_ss.channels;
386
387 if (u->buf4_samples < n_samples)
388 u->buf4 = pa_xrealloc(u->buf4, sizeof(float) * (u->buf4_samples = n_samples));
389
390 u->from_float32ne_func(n_samples, input, u->buf4);
391
392 return u->buf4;
393 }
394
395 static void libsamplerate_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
396 struct impl_libsamplerate *u;
397 float *buf, *input;
398 unsigned n_frames;
399
400 assert(r);
401 assert(in);
402 assert(out);
403 assert(in->length);
404 assert(in->memblock);
405 assert(in->length % r->i_fz == 0);
406 assert(r->impl_data);
407
408 u = r->impl_data;
409
410 buf = input = (float*) ((uint8_t*) in->memblock->data + in->index);
411 n_frames = in->length / r->i_fz;
412 assert(n_frames > 0);
413
414 buf = convert_to_float(r, buf, n_frames);
415 buf = remap_channels(r, buf, n_frames);
416 buf = resample(r, buf, &n_frames);
417
418 if (n_frames) {
419 buf = convert_from_float(r, buf, n_frames);
420
421 if (buf == input) {
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;
426 } else {
427 float **p = NULL;
428
429 out->length = n_frames * r->o_fz;
430 out->index = 0;
431
432 if (buf == u->buf1) {
433 p = &u->buf1;
434 u->buf1_samples = 0;
435 } else if (buf == u->buf2) {
436 p = &u->buf2;
437 u->buf2_samples = 0;
438 } else if (buf == u->buf3) {
439 p = &u->buf3;
440 u->buf3_samples = 0;
441 } else if (buf == u->buf4) {
442 p = &u->buf4;
443 u->buf4_samples = 0;
444 }
445
446 assert(p);
447
448 /* Take the existing buffer and make it a memblock */
449 out->memblock = pa_memblock_new_dynamic(*p, out->length, r->memblock_stat);
450 *p = NULL;
451 }
452 } else {
453 out->memblock = NULL;
454 out->index = out->length = 0;
455 }
456 }
457
458 static void libsamplerate_update_input_rate(pa_resampler *r, uint32_t rate) {
459 struct impl_libsamplerate *u;
460
461 assert(r);
462 assert(rate > 0);
463 assert(r->impl_data);
464 u = r->impl_data;
465
466 if (!u->src_state) {
467 int err;
468 u->src_state = src_new(r->resample_method, r->o_ss.channels, &err);
469 assert(u->src_state);
470 } else {
471 int ret = src_set_ratio(u->src_state, (double) r->o_ss.rate / rate);
472 assert(ret == 0);
473 }
474 }
475
476 static int libsamplerate_init(pa_resampler *r) {
477 struct impl_libsamplerate *u = NULL;
478 int err;
479
480 r->impl_data = u = pa_xnew(struct impl_libsamplerate, 1);
481
482 u->buf1 = u->buf2 = u->buf3 = u->buf4 = NULL;
483 u->buf1_samples = u->buf2_samples = u->buf3_samples = u->buf4_samples = 0;
484
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)))
488 goto fail;
489
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)))
493 goto fail;
494
495 if (r->o_ss.rate == r->i_ss.rate)
496 u->src_state = NULL;
497 else if (!(u->src_state = src_new(r->resample_method, r->o_ss.channels, &err)))
498 goto fail;
499
500 r->impl_free = libsamplerate_free;
501 r->impl_update_input_rate = libsamplerate_update_input_rate;
502 r->impl_run = libsamplerate_run;
503
504 calc_map_table(r);
505
506 return 0;
507
508 fail:
509 pa_xfree(u);
510 return -1;
511 }
512
513 /* Trivial implementation */
514
515 static void trivial_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
516 size_t fz;
517 unsigned n_frames;
518 struct impl_trivial *u;
519
520 assert(r);
521 assert(in);
522 assert(out);
523 assert(r->impl_data);
524
525 u = r->impl_data;
526
527 fz = r->i_fz;
528 assert(fz == r->o_fz);
529
530 n_frames = in->length/fz;
531
532 if (r->i_ss.rate == r->o_ss.rate) {
533
534 /* In case there's no diefference in sample types, do nothing */
535 *out = *in;
536 pa_memblock_ref(out->memblock);
537
538 u->o_counter += n_frames;
539 } else {
540 /* Do real resampling */
541 size_t l;
542 unsigned o_index;
543
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;
546
547 out->index = 0;
548 out->memblock = pa_memblock_new(l, r->memblock_stat);
549
550 for (o_index = 0;; o_index++, u->o_counter++) {
551 unsigned j;
552
553 j = (u->o_counter * r->i_ss.rate / r->o_ss.rate);
554 j = j > u->i_counter ? j - u->i_counter : 0;
555
556 if (j >= n_frames)
557 break;
558
559 assert(o_index*fz < out->memblock->length);
560
561 memcpy((uint8_t*) out->memblock->data + fz*o_index,
562 (uint8_t*) in->memblock->data + in->index + fz*j, fz);
563
564 }
565
566 out->length = o_index*fz;
567 }
568
569 u->i_counter += n_frames;
570
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;
576 }
577 }
578
579 static void trivial_free(pa_resampler *r) {
580 assert(r);
581
582 pa_xfree(r->impl_data);
583 }
584
585 static void trivial_update_input_rate(pa_resampler *r, uint32_t rate) {
586 struct impl_trivial *u;
587
588 assert(r);
589 assert(rate > 0);
590 assert(r->impl_data);
591
592 u = r->impl_data;
593 u->i_counter = 0;
594 u->o_counter = 0;
595 }
596
597 static int trivial_init(pa_resampler*r) {
598 struct impl_trivial *u;
599
600 assert(r);
601 assert(r->i_ss.format == r->o_ss.format);
602 assert(r->i_ss.channels == r->o_ss.channels);
603
604 r->impl_data = u = pa_xnew(struct impl_trivial, 1);
605 u->o_counter = u->i_counter = 0;
606
607 r->impl_run = trivial_run;
608 r->impl_free = trivial_free;
609 r->impl_update_input_rate = trivial_update_input_rate;
610
611 return 0;
612 }
613
614