]> code.delx.au - pulseaudio/blob - src/pulsecore/resampler.c
resampler: Use more descriptive buffer names.
[pulseaudio] / src / pulsecore / resampler.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.1 of the License,
9 or (at your option) any later version.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
30 #endif
31
32 #ifdef HAVE_SPEEX
33 #include <speex/speex_resampler.h>
34 #endif
35
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/remap.h>
42
43 #include "ffmpeg/avcodec.h"
44
45 #include "resampler.h"
46
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
49
50 struct pa_resampler {
51 pa_resample_method_t method;
52 pa_resample_flags_t flags;
53
54 pa_sample_spec i_ss, o_ss;
55 pa_channel_map i_cm, o_cm;
56 size_t i_fz, o_fz, w_sz;
57 pa_mempool *mempool;
58
59 pa_memchunk to_work_format_buf;
60 pa_memchunk remap_buf;
61 pa_memchunk resample_buf;
62 pa_memchunk from_work_format_buf;
63 unsigned to_work_format_buf_samples;
64 unsigned remap_buf_samples;
65 unsigned resample_buf_samples;
66 unsigned from_work_format_buf_samples;
67
68 pa_sample_format_t work_format;
69
70 pa_convert_func_t to_work_format_func;
71 pa_convert_func_t from_work_format_func;
72
73 pa_remap_t remap;
74 pa_bool_t map_required;
75
76 void (*impl_free)(pa_resampler *r);
77 void (*impl_update_rates)(pa_resampler *r);
78 void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
79 void (*impl_reset)(pa_resampler *r);
80
81 struct { /* data specific to the trivial resampler */
82 unsigned o_counter;
83 unsigned i_counter;
84 } trivial;
85
86 struct { /* data specific to the peak finder pseudo resampler */
87 unsigned o_counter;
88 unsigned i_counter;
89
90 float max_f[PA_CHANNELS_MAX];
91 int16_t max_i[PA_CHANNELS_MAX];
92
93 } peaks;
94
95 #ifdef HAVE_LIBSAMPLERATE
96 struct { /* data specific to libsamplerate */
97 SRC_STATE *state;
98 } src;
99 #endif
100
101 #ifdef HAVE_SPEEX
102 struct { /* data specific to speex */
103 SpeexResamplerState* state;
104 } speex;
105 #endif
106
107 struct { /* data specific to ffmpeg */
108 struct AVResampleContext *state;
109 pa_memchunk buf[PA_CHANNELS_MAX];
110 } ffmpeg;
111 };
112
113 static int copy_init(pa_resampler *r);
114 static int trivial_init(pa_resampler*r);
115 #ifdef HAVE_SPEEX
116 static int speex_init(pa_resampler*r);
117 #endif
118 static int ffmpeg_init(pa_resampler*r);
119 static int peaks_init(pa_resampler*r);
120 #ifdef HAVE_LIBSAMPLERATE
121 static int libsamplerate_init(pa_resampler*r);
122 #endif
123
124 static void calc_map_table(pa_resampler *r);
125
126 static int (* const init_table[])(pa_resampler*r) = {
127 #ifdef HAVE_LIBSAMPLERATE
128 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,
129 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
130 [PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,
131 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,
132 [PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,
133 #else
134 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = NULL,
135 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
136 [PA_RESAMPLER_SRC_SINC_FASTEST] = NULL,
137 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = NULL,
138 [PA_RESAMPLER_SRC_LINEAR] = NULL,
139 #endif
140 [PA_RESAMPLER_TRIVIAL] = trivial_init,
141 #ifdef HAVE_SPEEX
142 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = speex_init,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = speex_init,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = speex_init,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = speex_init,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = speex_init,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = speex_init,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = speex_init,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = speex_init,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,
153 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = speex_init,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = speex_init,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = speex_init,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = speex_init,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = speex_init,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = speex_init,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = speex_init,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = speex_init,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = speex_init,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,
164 #else
165 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = NULL,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = NULL,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = NULL,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = NULL,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = NULL,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = NULL,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = NULL,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = NULL,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = NULL,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = NULL,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = NULL,
176 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = NULL,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = NULL,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = NULL,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = NULL,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = NULL,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = NULL,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = NULL,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = NULL,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = NULL,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = NULL,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = NULL,
187 #endif
188 [PA_RESAMPLER_FFMPEG] = ffmpeg_init,
189 [PA_RESAMPLER_AUTO] = NULL,
190 [PA_RESAMPLER_COPY] = copy_init,
191 [PA_RESAMPLER_PEAKS] = peaks_init,
192 };
193
194 pa_resampler* pa_resampler_new(
195 pa_mempool *pool,
196 const pa_sample_spec *a,
197 const pa_channel_map *am,
198 const pa_sample_spec *b,
199 const pa_channel_map *bm,
200 pa_resample_method_t method,
201 pa_resample_flags_t flags) {
202
203 pa_resampler *r = NULL;
204
205 pa_assert(pool);
206 pa_assert(a);
207 pa_assert(b);
208 pa_assert(pa_sample_spec_valid(a));
209 pa_assert(pa_sample_spec_valid(b));
210 pa_assert(method >= 0);
211 pa_assert(method < PA_RESAMPLER_MAX);
212
213 /* Fix method */
214
215 if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && a->rate == b->rate) {
216 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
217 method = PA_RESAMPLER_COPY;
218 }
219
220 if (!pa_resample_method_supported(method)) {
221 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
222 method = PA_RESAMPLER_AUTO;
223 }
224
225 if (method == PA_RESAMPLER_FFMPEG && (flags & PA_RESAMPLER_VARIABLE_RATE)) {
226 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
227 method = PA_RESAMPLER_AUTO;
228 }
229
230 if (method == PA_RESAMPLER_COPY && ((flags & PA_RESAMPLER_VARIABLE_RATE) || a->rate != b->rate)) {
231 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
232 method = PA_RESAMPLER_AUTO;
233 }
234
235 if (method == PA_RESAMPLER_AUTO) {
236 #ifdef HAVE_SPEEX
237 method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
238 #else
239 method = PA_RESAMPLER_FFMPEG;
240 #endif
241 }
242
243 r = pa_xnew0(pa_resampler, 1);
244 r->mempool = pool;
245 r->method = method;
246 r->flags = flags;
247
248 /* Fill sample specs */
249 r->i_ss = *a;
250 r->o_ss = *b;
251
252 /* set up the remap structure */
253 r->remap.i_ss = &r->i_ss;
254 r->remap.o_ss = &r->o_ss;
255 r->remap.format = &r->work_format;
256
257 if (am)
258 r->i_cm = *am;
259 else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
260 goto fail;
261
262 if (bm)
263 r->o_cm = *bm;
264 else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
265 goto fail;
266
267 r->i_fz = pa_frame_size(a);
268 r->o_fz = pa_frame_size(b);
269
270 calc_map_table(r);
271
272 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
273
274 if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
275 (method == PA_RESAMPLER_FFMPEG))
276 r->work_format = PA_SAMPLE_S16NE;
277 else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
278
279 if (r->map_required || a->format != b->format || method == PA_RESAMPLER_PEAKS) {
280
281 if (a->format == PA_SAMPLE_S32NE || a->format == PA_SAMPLE_S32RE ||
282 a->format == PA_SAMPLE_FLOAT32NE || a->format == PA_SAMPLE_FLOAT32RE ||
283 a->format == PA_SAMPLE_S24NE || a->format == PA_SAMPLE_S24RE ||
284 a->format == PA_SAMPLE_S24_32NE || a->format == PA_SAMPLE_S24_32RE ||
285 b->format == PA_SAMPLE_S32NE || b->format == PA_SAMPLE_S32RE ||
286 b->format == PA_SAMPLE_FLOAT32NE || b->format == PA_SAMPLE_FLOAT32RE ||
287 b->format == PA_SAMPLE_S24NE || b->format == PA_SAMPLE_S24RE ||
288 b->format == PA_SAMPLE_S24_32NE || b->format == PA_SAMPLE_S24_32RE)
289 r->work_format = PA_SAMPLE_FLOAT32NE;
290 else
291 r->work_format = PA_SAMPLE_S16NE;
292
293 } else
294 r->work_format = a->format;
295
296 } else
297 r->work_format = PA_SAMPLE_FLOAT32NE;
298
299 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
300
301 r->w_sz = pa_sample_size_of_format(r->work_format);
302
303 if (r->i_ss.format != r->work_format) {
304 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
305 if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
306 goto fail;
307 } else {
308 pa_assert(r->work_format == PA_SAMPLE_S16NE);
309 if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
310 goto fail;
311 }
312 }
313
314 if (r->o_ss.format != r->work_format) {
315 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
316 if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
317 goto fail;
318 } else {
319 pa_assert(r->work_format == PA_SAMPLE_S16NE);
320 if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
321 goto fail;
322 }
323 }
324
325 /* initialize implementation */
326 if (init_table[method](r) < 0)
327 goto fail;
328
329 return r;
330
331 fail:
332 pa_xfree(r);
333
334 return NULL;
335 }
336
337 void pa_resampler_free(pa_resampler *r) {
338 pa_assert(r);
339
340 if (r->impl_free)
341 r->impl_free(r);
342
343 if (r->to_work_format_buf.memblock)
344 pa_memblock_unref(r->to_work_format_buf.memblock);
345 if (r->remap_buf.memblock)
346 pa_memblock_unref(r->remap_buf.memblock);
347 if (r->resample_buf.memblock)
348 pa_memblock_unref(r->resample_buf.memblock);
349 if (r->from_work_format_buf.memblock)
350 pa_memblock_unref(r->from_work_format_buf.memblock);
351
352 pa_xfree(r);
353 }
354
355 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
356 pa_assert(r);
357 pa_assert(rate > 0);
358
359 if (r->i_ss.rate == rate)
360 return;
361
362 r->i_ss.rate = rate;
363
364 r->impl_update_rates(r);
365 }
366
367 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
368 pa_assert(r);
369 pa_assert(rate > 0);
370
371 if (r->o_ss.rate == rate)
372 return;
373
374 r->o_ss.rate = rate;
375
376 r->impl_update_rates(r);
377 }
378
379 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
380 pa_assert(r);
381
382 /* Let's round up here */
383
384 return (((((out_length + r->o_fz-1) / r->o_fz) * r->i_ss.rate) + r->o_ss.rate-1) / r->o_ss.rate) * r->i_fz;
385 }
386
387 size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
388 pa_assert(r);
389
390 /* Let's round up here */
391
392 return (((((in_length + r->i_fz-1) / r->i_fz) * r->o_ss.rate) + r->i_ss.rate-1) / r->i_ss.rate) * r->o_fz;
393 }
394
395 size_t pa_resampler_max_block_size(pa_resampler *r) {
396 size_t block_size_max;
397 pa_sample_spec ss;
398 size_t fs;
399
400 pa_assert(r);
401
402 block_size_max = pa_mempool_block_size_max(r->mempool);
403
404 /* We deduce the "largest" sample spec we're using during the
405 * conversion */
406 ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
407
408 /* We silently assume that the format enum is ordered by size */
409 ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
410 ss.format = PA_MAX(ss.format, r->work_format);
411
412 ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
413
414 fs = pa_frame_size(&ss);
415
416 return (((block_size_max/fs - EXTRA_FRAMES)*r->i_ss.rate)/ss.rate)*r->i_fz;
417 }
418
419 void pa_resampler_reset(pa_resampler *r) {
420 pa_assert(r);
421
422 if (r->impl_reset)
423 r->impl_reset(r);
424 }
425
426 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
427 pa_assert(r);
428
429 return r->method;
430 }
431
432 const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
433 pa_assert(r);
434
435 return &r->i_cm;
436 }
437
438 const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
439 pa_assert(r);
440
441 return &r->i_ss;
442 }
443
444 const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
445 pa_assert(r);
446
447 return &r->o_cm;
448 }
449
450 const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
451 pa_assert(r);
452
453 return &r->o_ss;
454 }
455
456 static const char * const resample_methods[] = {
457 "src-sinc-best-quality",
458 "src-sinc-medium-quality",
459 "src-sinc-fastest",
460 "src-zero-order-hold",
461 "src-linear",
462 "trivial",
463 "speex-float-0",
464 "speex-float-1",
465 "speex-float-2",
466 "speex-float-3",
467 "speex-float-4",
468 "speex-float-5",
469 "speex-float-6",
470 "speex-float-7",
471 "speex-float-8",
472 "speex-float-9",
473 "speex-float-10",
474 "speex-fixed-0",
475 "speex-fixed-1",
476 "speex-fixed-2",
477 "speex-fixed-3",
478 "speex-fixed-4",
479 "speex-fixed-5",
480 "speex-fixed-6",
481 "speex-fixed-7",
482 "speex-fixed-8",
483 "speex-fixed-9",
484 "speex-fixed-10",
485 "ffmpeg",
486 "auto",
487 "copy",
488 "peaks"
489 };
490
491 const char *pa_resample_method_to_string(pa_resample_method_t m) {
492
493 if (m < 0 || m >= PA_RESAMPLER_MAX)
494 return NULL;
495
496 return resample_methods[m];
497 }
498
499 int pa_resample_method_supported(pa_resample_method_t m) {
500
501 if (m < 0 || m >= PA_RESAMPLER_MAX)
502 return 0;
503
504 #ifndef HAVE_LIBSAMPLERATE
505 if (m <= PA_RESAMPLER_SRC_LINEAR)
506 return 0;
507 #endif
508
509 #ifndef HAVE_SPEEX
510 if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
511 return 0;
512 if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
513 return 0;
514 #endif
515
516 return 1;
517 }
518
519 pa_resample_method_t pa_parse_resample_method(const char *string) {
520 pa_resample_method_t m;
521
522 pa_assert(string);
523
524 for (m = 0; m < PA_RESAMPLER_MAX; m++)
525 if (!strcmp(string, resample_methods[m]))
526 return m;
527
528 if (!strcmp(string, "speex-fixed"))
529 return PA_RESAMPLER_SPEEX_FIXED_BASE + 3;
530
531 if (!strcmp(string, "speex-float"))
532 return PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
533
534 return PA_RESAMPLER_INVALID;
535 }
536
537 static pa_bool_t on_left(pa_channel_position_t p) {
538
539 return
540 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
541 p == PA_CHANNEL_POSITION_REAR_LEFT ||
542 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
543 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
544 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
545 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
546 }
547
548 static pa_bool_t on_right(pa_channel_position_t p) {
549
550 return
551 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
552 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
553 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
554 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
555 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
556 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
557 }
558
559 static pa_bool_t on_center(pa_channel_position_t p) {
560
561 return
562 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
563 p == PA_CHANNEL_POSITION_REAR_CENTER ||
564 p == PA_CHANNEL_POSITION_TOP_CENTER ||
565 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
566 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
567 }
568
569 static pa_bool_t on_lfe(pa_channel_position_t p) {
570 return
571 p == PA_CHANNEL_POSITION_LFE;
572 }
573
574 static pa_bool_t on_front(pa_channel_position_t p) {
575 return
576 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
577 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
578 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
579 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
580 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
581 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
582 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
583 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
584 }
585
586 static pa_bool_t on_rear(pa_channel_position_t p) {
587 return
588 p == PA_CHANNEL_POSITION_REAR_LEFT ||
589 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
590 p == PA_CHANNEL_POSITION_REAR_CENTER ||
591 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
592 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
593 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
594 }
595
596 static pa_bool_t on_side(pa_channel_position_t p) {
597 return
598 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
599 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
600 p == PA_CHANNEL_POSITION_TOP_CENTER;
601 }
602
603 enum {
604 ON_FRONT,
605 ON_REAR,
606 ON_SIDE,
607 ON_OTHER
608 };
609
610 static int front_rear_side(pa_channel_position_t p) {
611 if (on_front(p))
612 return ON_FRONT;
613 if (on_rear(p))
614 return ON_REAR;
615 if (on_side(p))
616 return ON_SIDE;
617 return ON_OTHER;
618 }
619
620 static void calc_map_table(pa_resampler *r) {
621 unsigned oc, ic;
622 unsigned n_oc, n_ic;
623 pa_bool_t ic_connected[PA_CHANNELS_MAX];
624 pa_bool_t remix;
625 pa_strbuf *s;
626 char *t;
627 pa_remap_t *m;
628
629 pa_assert(r);
630
631 if (!(r->map_required = (r->i_ss.channels != r->o_ss.channels || (!(r->flags & PA_RESAMPLER_NO_REMAP) && !pa_channel_map_equal(&r->i_cm, &r->o_cm)))))
632 return;
633
634 m = &r->remap;
635
636 n_oc = r->o_ss.channels;
637 n_ic = r->i_ss.channels;
638
639 memset(m->map_table_f, 0, sizeof(m->map_table_f));
640 memset(m->map_table_i, 0, sizeof(m->map_table_i));
641
642 memset(ic_connected, 0, sizeof(ic_connected));
643 remix = (r->flags & (PA_RESAMPLER_NO_REMAP|PA_RESAMPLER_NO_REMIX)) == 0;
644
645 for (oc = 0; oc < n_oc; oc++) {
646 pa_bool_t oc_connected = FALSE;
647 pa_channel_position_t b = r->o_cm.map[oc];
648
649 for (ic = 0; ic < n_ic; ic++) {
650 pa_channel_position_t a = r->i_cm.map[ic];
651
652 if (r->flags & PA_RESAMPLER_NO_REMAP) {
653 /* We shall not do any remapping. Hence, just check by index */
654
655 if (ic == oc)
656 m->map_table_f[oc][ic] = 1.0;
657
658 continue;
659 }
660
661 if (r->flags & PA_RESAMPLER_NO_REMIX) {
662 /* We shall not do any remixing. Hence, just check by name */
663
664 if (a == b)
665 m->map_table_f[oc][ic] = 1.0;
666
667 continue;
668 }
669
670 pa_assert(remix);
671
672 /* OK, we shall do the full monty: upmixing and
673 * downmixing. Our algorithm is relatively simple, does
674 * not do spacialization, delay elements or apply lowpass
675 * filters for LFE. Patches are always welcome,
676 * though. Oh, and it doesn't do any matrix
677 * decoding. (Which probably wouldn't make any sense
678 * anyway.)
679 *
680 * This code is not idempotent: downmixing an upmixed
681 * stereo stream is not identical to the original. The
682 * volume will not match, and the two channels will be a
683 * linear combination of both.
684 *
685 * This is loosely based on random suggestions found on the
686 * Internet, such as this:
687 * http://www.halfgaar.net/surround-sound-in-linux and the
688 * alsa upmix plugin.
689 *
690 * The algorithm works basically like this:
691 *
692 * 1) Connect all channels with matching names.
693 *
694 * 2) Mono Handling:
695 * S:Mono: Copy into all D:channels
696 * D:Mono: Copy in all S:channels
697 *
698 * 3) Mix D:Left, D:Right:
699 * D:Left: If not connected, avg all S:Left
700 * D:Right: If not connected, avg all S:Right
701 *
702 * 4) Mix D:Center
703 * If not connected, avg all S:Center
704 * If still not connected, avg all S:Left, S:Right
705 *
706 * 5) Mix D:LFE
707 * If not connected, avg all S:*
708 *
709 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If
710 * not connected, mix into all D:left and all D:right
711 * channels. Gain is 0.1, the current left and right
712 * should be multiplied by 0.9.
713 *
714 * 7) Make sure S:Center, S:LFE is used:
715 *
716 * S:Center, S:LFE: If not connected, mix into all
717 * D:left, all D:right, all D:center channels, gain is
718 * 0.375. The current (as result of 1..6) factors
719 * should be multiplied by 0.75. (Alt. suggestion: 0.25
720 * vs. 0.5) If C-front is only mixed into
721 * L-front/R-front if available, otherwise into all L/R
722 * channels. Similarly for C-rear.
723 *
724 * S: and D: shall relate to the source resp. destination channels.
725 *
726 * Rationale: 1, 2 are probably obvious. For 3: this
727 * copies front to rear if needed. For 4: we try to find
728 * some suitable C source for C, if we don't find any, we
729 * avg L and R. For 5: LFE is mixed from all channels. For
730 * 6: the rear channels should not be dropped entirely,
731 * however have only minimal impact. For 7: movies usually
732 * encode speech on the center channel. Thus we have to
733 * make sure this channel is distributed to L and R if not
734 * available in the output. Also, LFE is used to achieve a
735 * greater dynamic range, and thus we should try to do our
736 * best to pass it to L+R.
737 */
738
739 if (a == b || a == PA_CHANNEL_POSITION_MONO || b == PA_CHANNEL_POSITION_MONO) {
740 m->map_table_f[oc][ic] = 1.0;
741
742 oc_connected = TRUE;
743 ic_connected[ic] = TRUE;
744 }
745 }
746
747 if (!oc_connected && remix) {
748 /* OK, we shall remix */
749
750 /* Try to find matching input ports for this output port */
751
752 if (on_left(b)) {
753 unsigned n = 0;
754
755 /* We are not connected and on the left side, let's
756 * average all left side input channels. */
757
758 for (ic = 0; ic < n_ic; ic++)
759 if (on_left(r->i_cm.map[ic]))
760 n++;
761
762 if (n > 0)
763 for (ic = 0; ic < n_ic; ic++)
764 if (on_left(r->i_cm.map[ic])) {
765 m->map_table_f[oc][ic] = 1.0f / (float) n;
766 ic_connected[ic] = TRUE;
767 }
768
769 /* We ignore the case where there is no left input
770 * channel. Something is really wrong in this case
771 * anyway. */
772
773 } else if (on_right(b)) {
774 unsigned n = 0;
775
776 /* We are not connected and on the right side, let's
777 * average all right side input channels. */
778
779 for (ic = 0; ic < n_ic; ic++)
780 if (on_right(r->i_cm.map[ic]))
781 n++;
782
783 if (n > 0)
784 for (ic = 0; ic < n_ic; ic++)
785 if (on_right(r->i_cm.map[ic])) {
786 m->map_table_f[oc][ic] = 1.0f / (float) n;
787 ic_connected[ic] = TRUE;
788 }
789
790 /* We ignore the case where there is no right input
791 * channel. Something is really wrong in this case
792 * anyway. */
793
794 } else if (on_center(b)) {
795 unsigned n = 0;
796
797 /* We are not connected and at the center. Let's
798 * average all center input channels. */
799
800 for (ic = 0; ic < n_ic; ic++)
801 if (on_center(r->i_cm.map[ic]))
802 n++;
803
804 if (n > 0) {
805 for (ic = 0; ic < n_ic; ic++)
806 if (on_center(r->i_cm.map[ic])) {
807 m->map_table_f[oc][ic] = 1.0f / (float) n;
808 ic_connected[ic] = TRUE;
809 }
810 } else {
811
812 /* Hmm, no center channel around, let's synthesize
813 * it by mixing L and R.*/
814
815 n = 0;
816
817 for (ic = 0; ic < n_ic; ic++)
818 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic]))
819 n++;
820
821 if (n > 0)
822 for (ic = 0; ic < n_ic; ic++)
823 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
824 m->map_table_f[oc][ic] = 1.0f / (float) n;
825 ic_connected[ic] = TRUE;
826 }
827
828 /* We ignore the case where there is not even a
829 * left or right input channel. Something is
830 * really wrong in this case anyway. */
831 }
832
833 } else if (on_lfe(b)) {
834
835 /* We are not connected and an LFE. Let's average all
836 * channels for LFE. */
837
838 for (ic = 0; ic < n_ic; ic++) {
839
840 if (!(r->flags & PA_RESAMPLER_NO_LFE))
841 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
842 else
843 m->map_table_f[oc][ic] = 0;
844
845 /* Please note that a channel connected to LFE
846 * doesn't really count as connected. */
847 }
848 }
849 }
850 }
851
852 if (remix) {
853 unsigned
854 ic_unconnected_left = 0,
855 ic_unconnected_right = 0,
856 ic_unconnected_center = 0,
857 ic_unconnected_lfe = 0;
858
859 for (ic = 0; ic < n_ic; ic++) {
860 pa_channel_position_t a = r->i_cm.map[ic];
861
862 if (ic_connected[ic])
863 continue;
864
865 if (on_left(a))
866 ic_unconnected_left++;
867 else if (on_right(a))
868 ic_unconnected_right++;
869 else if (on_center(a))
870 ic_unconnected_center++;
871 else if (on_lfe(a))
872 ic_unconnected_lfe++;
873 }
874
875 if (ic_unconnected_left > 0) {
876
877 /* OK, so there are unconnected input channels on the
878 * left. Let's multiply all already connected channels on
879 * the left side by .9 and add in our averaged unconnected
880 * channels multiplied by .1 */
881
882 for (oc = 0; oc < n_oc; oc++) {
883
884 if (!on_left(r->o_cm.map[oc]))
885 continue;
886
887 for (ic = 0; ic < n_ic; ic++) {
888
889 if (ic_connected[ic]) {
890 m->map_table_f[oc][ic] *= .9f;
891 continue;
892 }
893
894 if (on_left(r->i_cm.map[ic]))
895 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left;
896 }
897 }
898 }
899
900 if (ic_unconnected_right > 0) {
901
902 /* OK, so there are unconnected input channels on the
903 * right. Let's multiply all already connected channels on
904 * the right side by .9 and add in our averaged unconnected
905 * channels multiplied by .1 */
906
907 for (oc = 0; oc < n_oc; oc++) {
908
909 if (!on_right(r->o_cm.map[oc]))
910 continue;
911
912 for (ic = 0; ic < n_ic; ic++) {
913
914 if (ic_connected[ic]) {
915 m->map_table_f[oc][ic] *= .9f;
916 continue;
917 }
918
919 if (on_right(r->i_cm.map[ic]))
920 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right;
921 }
922 }
923 }
924
925 if (ic_unconnected_center > 0) {
926 pa_bool_t mixed_in = FALSE;
927
928 /* OK, so there are unconnected input channels on the
929 * center. Let's multiply all already connected channels on
930 * the center side by .9 and add in our averaged unconnected
931 * channels multiplied by .1 */
932
933 for (oc = 0; oc < n_oc; oc++) {
934
935 if (!on_center(r->o_cm.map[oc]))
936 continue;
937
938 for (ic = 0; ic < n_ic; ic++) {
939
940 if (ic_connected[ic]) {
941 m->map_table_f[oc][ic] *= .9f;
942 continue;
943 }
944
945 if (on_center(r->i_cm.map[ic])) {
946 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center;
947 mixed_in = TRUE;
948 }
949 }
950 }
951
952 if (!mixed_in) {
953 unsigned ncenter[PA_CHANNELS_MAX];
954 pa_bool_t found_frs[PA_CHANNELS_MAX];
955
956 memset(ncenter, 0, sizeof(ncenter));
957 memset(found_frs, 0, sizeof(found_frs));
958
959 /* Hmm, as it appears there was no center channel we
960 could mix our center channel in. In this case, mix
961 it into left and right. Using .375 and 0.75 as
962 factors. */
963
964 for (ic = 0; ic < n_ic; ic++) {
965
966 if (ic_connected[ic])
967 continue;
968
969 if (!on_center(r->i_cm.map[ic]))
970 continue;
971
972 for (oc = 0; oc < n_oc; oc++) {
973
974 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
975 continue;
976
977 if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
978 found_frs[ic] = TRUE;
979 break;
980 }
981 }
982
983 for (oc = 0; oc < n_oc; oc++) {
984
985 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
986 continue;
987
988 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
989 ncenter[oc]++;
990 }
991 }
992
993 for (oc = 0; oc < n_oc; oc++) {
994
995 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
996 continue;
997
998 if (ncenter[oc] <= 0)
999 continue;
1000
1001 for (ic = 0; ic < n_ic; ic++) {
1002
1003 if (ic_connected[ic]) {
1004 m->map_table_f[oc][ic] *= .75f;
1005 continue;
1006 }
1007
1008 if (!on_center(r->i_cm.map[ic]))
1009 continue;
1010
1011 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1012 m->map_table_f[oc][ic] = .375f / (float) ncenter[oc];
1013 }
1014 }
1015 }
1016 }
1017
1018 if (ic_unconnected_lfe > 0 && !(r->flags & PA_RESAMPLER_NO_LFE)) {
1019
1020 /* OK, so there is an unconnected LFE channel. Let's mix
1021 * it into all channels, with factor 0.375 */
1022
1023 for (ic = 0; ic < n_ic; ic++) {
1024
1025 if (!on_lfe(r->i_cm.map[ic]))
1026 continue;
1027
1028 for (oc = 0; oc < n_oc; oc++)
1029 m->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe;
1030 }
1031 }
1032 }
1033 /* make an 16:16 int version of the matrix */
1034 for (oc = 0; oc < n_oc; oc++)
1035 for (ic = 0; ic < n_ic; ic++)
1036 m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
1037
1038 s = pa_strbuf_new();
1039
1040 pa_strbuf_printf(s, " ");
1041 for (ic = 0; ic < n_ic; ic++)
1042 pa_strbuf_printf(s, " I%02u ", ic);
1043 pa_strbuf_puts(s, "\n +");
1044
1045 for (ic = 0; ic < n_ic; ic++)
1046 pa_strbuf_printf(s, "------");
1047 pa_strbuf_puts(s, "\n");
1048
1049 for (oc = 0; oc < n_oc; oc++) {
1050 pa_strbuf_printf(s, "O%02u |", oc);
1051
1052 for (ic = 0; ic < n_ic; ic++)
1053 pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
1054
1055 pa_strbuf_puts(s, "\n");
1056 }
1057
1058 pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s));
1059 pa_xfree(t);
1060
1061 /* initialize the remapping function */
1062 pa_init_remap(m);
1063 }
1064
1065 static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
1066 unsigned n_samples;
1067 void *src, *dst;
1068
1069 pa_assert(r);
1070 pa_assert(input);
1071 pa_assert(input->memblock);
1072
1073 /* Convert the incoming sample into the work sample format and place them
1074 * in to_work_format_buf. */
1075
1076 if (!r->to_work_format_func || !input->length)
1077 return input;
1078
1079 n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
1080
1081 r->to_work_format_buf.index = 0;
1082 r->to_work_format_buf.length = r->w_sz * n_samples;
1083
1084 if (!r->to_work_format_buf.memblock || r->to_work_format_buf_samples < n_samples) {
1085 if (r->to_work_format_buf.memblock)
1086 pa_memblock_unref(r->to_work_format_buf.memblock);
1087
1088 r->to_work_format_buf_samples = n_samples;
1089 r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
1090 }
1091
1092 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1093 dst = (uint8_t*) pa_memblock_acquire(r->to_work_format_buf.memblock);
1094
1095 r->to_work_format_func(n_samples, src, dst);
1096
1097 pa_memblock_release(input->memblock);
1098 pa_memblock_release(r->to_work_format_buf.memblock);
1099
1100 return &r->to_work_format_buf;
1101 }
1102
1103 static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
1104 unsigned in_n_samples, out_n_samples, n_frames;
1105 void *src, *dst;
1106 pa_remap_t *remap;
1107
1108 pa_assert(r);
1109 pa_assert(input);
1110 pa_assert(input->memblock);
1111
1112 /* Remap channels and place the result in remap_buf. */
1113
1114 if (!r->map_required || !input->length)
1115 return input;
1116
1117 in_n_samples = (unsigned) (input->length / r->w_sz);
1118 n_frames = in_n_samples / r->i_ss.channels;
1119 out_n_samples = n_frames * r->o_ss.channels;
1120
1121 r->remap_buf.index = 0;
1122 r->remap_buf.length = r->w_sz * out_n_samples;
1123
1124 if (!r->remap_buf.memblock || r->remap_buf_samples < out_n_samples) {
1125 if (r->remap_buf.memblock)
1126 pa_memblock_unref(r->remap_buf.memblock);
1127
1128 r->remap_buf_samples = out_n_samples;
1129 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1130 }
1131
1132 src = ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1133 dst = pa_memblock_acquire(r->remap_buf.memblock);
1134
1135 remap = &r->remap;
1136
1137 pa_assert(remap->do_remap);
1138 remap->do_remap(remap, dst, src, n_frames);
1139
1140 pa_memblock_release(input->memblock);
1141 pa_memblock_release(r->remap_buf.memblock);
1142
1143 return &r->remap_buf;
1144 }
1145
1146 static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
1147 unsigned in_n_frames, in_n_samples;
1148 unsigned out_n_frames, out_n_samples;
1149
1150 pa_assert(r);
1151 pa_assert(input);
1152
1153 /* Resample the data and place the result in resample_buf. */
1154
1155 if (!r->impl_resample || !input->length)
1156 return input;
1157
1158 in_n_samples = (unsigned) (input->length / r->w_sz);
1159 in_n_frames = (unsigned) (in_n_samples / r->o_ss.channels);
1160
1161 out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
1162 out_n_samples = out_n_frames * r->o_ss.channels;
1163
1164 r->resample_buf.index = 0;
1165 r->resample_buf.length = r->w_sz * out_n_samples;
1166
1167 if (!r->resample_buf.memblock || r->resample_buf_samples < out_n_samples) {
1168 if (r->resample_buf.memblock)
1169 pa_memblock_unref(r->resample_buf.memblock);
1170
1171 r->resample_buf_samples = out_n_samples;
1172 r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
1173 }
1174
1175 r->impl_resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
1176 r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
1177
1178 return &r->resample_buf;
1179 }
1180
1181 static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
1182 unsigned n_samples, n_frames;
1183 void *src, *dst;
1184
1185 pa_assert(r);
1186 pa_assert(input);
1187
1188 /* Convert the data into the correct sample type and place the result in
1189 * from_work_format_buf. */
1190
1191 if (!r->from_work_format_func || !input->length)
1192 return input;
1193
1194 n_samples = (unsigned) (input->length / r->w_sz);
1195 n_frames = n_samples / r->o_ss.channels;
1196
1197 r->from_work_format_buf.index = 0;
1198 r->from_work_format_buf.length = r->o_fz * n_frames;
1199
1200 if (!r->from_work_format_buf.memblock || r->from_work_format_buf_samples < n_samples) {
1201 if (r->from_work_format_buf.memblock)
1202 pa_memblock_unref(r->from_work_format_buf.memblock);
1203
1204 r->from_work_format_buf_samples = n_samples;
1205 r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
1206 }
1207
1208 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1209 dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
1210 r->from_work_format_func(n_samples, src, dst);
1211 pa_memblock_release(input->memblock);
1212 pa_memblock_release(r->from_work_format_buf.memblock);
1213
1214 r->from_work_format_buf.length = r->o_fz * n_frames;
1215
1216 return &r->from_work_format_buf;
1217 }
1218
1219 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
1220 pa_memchunk *buf;
1221
1222 pa_assert(r);
1223 pa_assert(in);
1224 pa_assert(out);
1225 pa_assert(in->length);
1226 pa_assert(in->memblock);
1227 pa_assert(in->length % r->i_fz == 0);
1228
1229 buf = (pa_memchunk*) in;
1230 buf = convert_to_work_format(r, buf);
1231 buf = remap_channels(r, buf);
1232 buf = resample(r, buf);
1233
1234 if (buf->length) {
1235 buf = convert_from_work_format(r, buf);
1236 *out = *buf;
1237
1238 if (buf == in)
1239 pa_memblock_ref(buf->memblock);
1240 else
1241 pa_memchunk_reset(buf);
1242 } else
1243 pa_memchunk_reset(out);
1244 }
1245
1246 /*** libsamplerate based implementation ***/
1247
1248 #ifdef HAVE_LIBSAMPLERATE
1249 static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1250 SRC_DATA data;
1251
1252 pa_assert(r);
1253 pa_assert(input);
1254 pa_assert(output);
1255 pa_assert(out_n_frames);
1256
1257 memset(&data, 0, sizeof(data));
1258
1259 data.data_in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1260 data.input_frames = (long int) in_n_frames;
1261
1262 data.data_out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
1263 data.output_frames = (long int) *out_n_frames;
1264
1265 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
1266 data.end_of_input = 0;
1267
1268 pa_assert_se(src_process(r->src.state, &data) == 0);
1269 pa_assert((unsigned) data.input_frames_used == in_n_frames);
1270
1271 pa_memblock_release(input->memblock);
1272 pa_memblock_release(output->memblock);
1273
1274 *out_n_frames = (unsigned) data.output_frames_gen;
1275 }
1276
1277 static void libsamplerate_update_rates(pa_resampler *r) {
1278 pa_assert(r);
1279
1280 pa_assert_se(src_set_ratio(r->src.state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
1281 }
1282
1283 static void libsamplerate_reset(pa_resampler *r) {
1284 pa_assert(r);
1285
1286 pa_assert_se(src_reset(r->src.state) == 0);
1287 }
1288
1289 static void libsamplerate_free(pa_resampler *r) {
1290 pa_assert(r);
1291
1292 if (r->src.state)
1293 src_delete(r->src.state);
1294 }
1295
1296 static int libsamplerate_init(pa_resampler *r) {
1297 int err;
1298
1299 pa_assert(r);
1300
1301 if (!(r->src.state = src_new(r->method, r->o_ss.channels, &err)))
1302 return -1;
1303
1304 r->impl_free = libsamplerate_free;
1305 r->impl_update_rates = libsamplerate_update_rates;
1306 r->impl_resample = libsamplerate_resample;
1307 r->impl_reset = libsamplerate_reset;
1308
1309 return 0;
1310 }
1311 #endif
1312
1313 #ifdef HAVE_SPEEX
1314 /*** speex based implementation ***/
1315
1316 static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1317 float *in, *out;
1318 uint32_t inf = in_n_frames, outf = *out_n_frames;
1319
1320 pa_assert(r);
1321 pa_assert(input);
1322 pa_assert(output);
1323 pa_assert(out_n_frames);
1324
1325 in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1326 out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
1327
1328 pa_assert_se(speex_resampler_process_interleaved_float(r->speex.state, in, &inf, out, &outf) == 0);
1329
1330 pa_memblock_release(input->memblock);
1331 pa_memblock_release(output->memblock);
1332
1333 pa_assert(inf == in_n_frames);
1334 *out_n_frames = outf;
1335 }
1336
1337 static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1338 int16_t *in, *out;
1339 uint32_t inf = in_n_frames, outf = *out_n_frames;
1340
1341 pa_assert(r);
1342 pa_assert(input);
1343 pa_assert(output);
1344 pa_assert(out_n_frames);
1345
1346 in = (int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1347 out = (int16_t*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
1348
1349 pa_assert_se(speex_resampler_process_interleaved_int(r->speex.state, in, &inf, out, &outf) == 0);
1350
1351 pa_memblock_release(input->memblock);
1352 pa_memblock_release(output->memblock);
1353
1354 pa_assert(inf == in_n_frames);
1355 *out_n_frames = outf;
1356 }
1357
1358 static void speex_update_rates(pa_resampler *r) {
1359 pa_assert(r);
1360
1361 pa_assert_se(speex_resampler_set_rate(r->speex.state, r->i_ss.rate, r->o_ss.rate) == 0);
1362 }
1363
1364 static void speex_reset(pa_resampler *r) {
1365 pa_assert(r);
1366
1367 pa_assert_se(speex_resampler_reset_mem(r->speex.state) == 0);
1368 }
1369
1370 static void speex_free(pa_resampler *r) {
1371 pa_assert(r);
1372
1373 if (!r->speex.state)
1374 return;
1375
1376 speex_resampler_destroy(r->speex.state);
1377 }
1378
1379 static int speex_init(pa_resampler *r) {
1380 int q, err;
1381
1382 pa_assert(r);
1383
1384 r->impl_free = speex_free;
1385 r->impl_update_rates = speex_update_rates;
1386 r->impl_reset = speex_reset;
1387
1388 if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
1389
1390 q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
1391 r->impl_resample = speex_resample_int;
1392
1393 } else {
1394 pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
1395
1396 q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
1397 r->impl_resample = speex_resample_float;
1398 }
1399
1400 pa_log_info("Choosing speex quality setting %i.", q);
1401
1402 if (!(r->speex.state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
1403 return -1;
1404
1405 return 0;
1406 }
1407 #endif
1408
1409 /* Trivial implementation */
1410
1411 static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1412 size_t fz;
1413 unsigned i_index, o_index;
1414 void *src, *dst;
1415
1416 pa_assert(r);
1417 pa_assert(input);
1418 pa_assert(output);
1419 pa_assert(out_n_frames);
1420
1421 fz = r->w_sz * r->o_ss.channels;
1422
1423 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1424 dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
1425
1426 for (o_index = 0;; o_index++, r->trivial.o_counter++) {
1427 i_index = (r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate;
1428 i_index = i_index > r->trivial.i_counter ? i_index - r->trivial.i_counter : 0;
1429
1430 if (i_index >= in_n_frames)
1431 break;
1432
1433 pa_assert_fp(o_index * fz < pa_memblock_get_length(output->memblock));
1434
1435 memcpy((uint8_t*) dst + fz * o_index, (uint8_t*) src + fz * i_index, (int) fz);
1436 }
1437
1438 pa_memblock_release(input->memblock);
1439 pa_memblock_release(output->memblock);
1440
1441 *out_n_frames = o_index;
1442
1443 r->trivial.i_counter += in_n_frames;
1444
1445 /* Normalize counters */
1446 while (r->trivial.i_counter >= r->i_ss.rate) {
1447 pa_assert(r->trivial.o_counter >= r->o_ss.rate);
1448
1449 r->trivial.i_counter -= r->i_ss.rate;
1450 r->trivial.o_counter -= r->o_ss.rate;
1451 }
1452 }
1453
1454 static void trivial_update_rates_or_reset(pa_resampler *r) {
1455 pa_assert(r);
1456
1457 r->trivial.i_counter = 0;
1458 r->trivial.o_counter = 0;
1459 }
1460
1461 static int trivial_init(pa_resampler*r) {
1462 pa_assert(r);
1463
1464 r->trivial.o_counter = r->trivial.i_counter = 0;
1465
1466 r->impl_resample = trivial_resample;
1467 r->impl_update_rates = trivial_update_rates_or_reset;
1468 r->impl_reset = trivial_update_rates_or_reset;
1469
1470 return 0;
1471 }
1472
1473 /* Peak finder implementation */
1474
1475 static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1476 unsigned c, o_index = 0;
1477 unsigned i, i_end = 0;
1478 void *src, *dst;
1479
1480 pa_assert(r);
1481 pa_assert(input);
1482 pa_assert(output);
1483 pa_assert(out_n_frames);
1484
1485 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1486 dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
1487
1488 i = (r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate;
1489 i = i > r->peaks.i_counter ? i - r->peaks.i_counter : 0;
1490
1491 while (i_end < in_n_frames) {
1492 i_end = ((r->peaks.o_counter+1) * r->i_ss.rate) / r->o_ss.rate;
1493 i_end = i_end > r->peaks.i_counter ? i_end - r->peaks.i_counter : 0;
1494
1495 pa_assert_fp(o_index * r->w_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
1496
1497 /* 1ch float is treated separately, because that is the common case */
1498 if (r->o_ss.channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
1499 float *s = (float*) src + i;
1500 float *d = (float*) dst + o_index;
1501
1502 for (; i < i_end && i < in_n_frames; i++) {
1503 float n = fabsf(*s++);
1504
1505 if (n > r->peaks.max_f[0])
1506 r->peaks.max_f[0] = n;
1507 }
1508
1509 if (i == i_end) {
1510 *d = r->peaks.max_f[0];
1511 r->peaks.max_f[0] = 0;
1512 o_index++, r->peaks.o_counter++;
1513 }
1514 } else if (r->work_format == PA_SAMPLE_S16NE) {
1515 int16_t *s = (int16_t*) src + r->i_ss.channels * i;
1516 int16_t *d = (int16_t*) dst + r->o_ss.channels * o_index;
1517
1518 for (; i < i_end && i < in_n_frames; i++)
1519 for (c = 0; c < r->o_ss.channels; c++) {
1520 int16_t n = abs(*s++);
1521
1522 if (n > r->peaks.max_i[c])
1523 r->peaks.max_i[c] = n;
1524 }
1525
1526 if (i == i_end) {
1527 for (c = 0; c < r->o_ss.channels; c++, d++) {
1528 *d = r->peaks.max_i[c];
1529 r->peaks.max_i[c] = 0;
1530 }
1531 o_index++, r->peaks.o_counter++;
1532 }
1533 } else {
1534 float *s = (float*) src + r->i_ss.channels * i;
1535 float *d = (float*) dst + r->o_ss.channels * o_index;
1536
1537 for (; i < i_end && i < in_n_frames; i++)
1538 for (c = 0; c < r->o_ss.channels; c++) {
1539 float n = fabsf(*s++);
1540
1541 if (n > r->peaks.max_f[c])
1542 r->peaks.max_f[c] = n;
1543 }
1544
1545 if (i == i_end) {
1546 for (c = 0; c < r->o_ss.channels; c++, d++) {
1547 *d = r->peaks.max_f[c];
1548 r->peaks.max_f[c] = 0;
1549 }
1550 o_index++, r->peaks.o_counter++;
1551 }
1552 }
1553 }
1554
1555 pa_memblock_release(input->memblock);
1556 pa_memblock_release(output->memblock);
1557
1558 *out_n_frames = o_index;
1559
1560 r->peaks.i_counter += in_n_frames;
1561
1562 /* Normalize counters */
1563 while (r->peaks.i_counter >= r->i_ss.rate) {
1564 pa_assert(r->peaks.o_counter >= r->o_ss.rate);
1565
1566 r->peaks.i_counter -= r->i_ss.rate;
1567 r->peaks.o_counter -= r->o_ss.rate;
1568 }
1569 }
1570
1571 static void peaks_update_rates_or_reset(pa_resampler *r) {
1572 pa_assert(r);
1573
1574 r->peaks.i_counter = 0;
1575 r->peaks.o_counter = 0;
1576 }
1577
1578 static int peaks_init(pa_resampler*r) {
1579 pa_assert(r);
1580 pa_assert(r->i_ss.rate >= r->o_ss.rate);
1581 pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
1582
1583 r->peaks.o_counter = r->peaks.i_counter = 0;
1584 memset(r->peaks.max_i, 0, sizeof(r->peaks.max_i));
1585 memset(r->peaks.max_f, 0, sizeof(r->peaks.max_f));
1586
1587 r->impl_resample = peaks_resample;
1588 r->impl_update_rates = peaks_update_rates_or_reset;
1589 r->impl_reset = peaks_update_rates_or_reset;
1590
1591 return 0;
1592 }
1593
1594 /*** ffmpeg based implementation ***/
1595
1596 static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1597 unsigned used_frames = 0, c;
1598
1599 pa_assert(r);
1600 pa_assert(input);
1601 pa_assert(output);
1602 pa_assert(out_n_frames);
1603
1604 for (c = 0; c < r->o_ss.channels; c++) {
1605 unsigned u;
1606 pa_memblock *b, *w;
1607 int16_t *p, *t, *k, *q, *s;
1608 int consumed_frames;
1609 unsigned in, l;
1610
1611 /* Allocate a new block */
1612 b = pa_memblock_new(r->mempool, r->ffmpeg.buf[c].length + in_n_frames * sizeof(int16_t));
1613 p = pa_memblock_acquire(b);
1614
1615 /* Copy the remaining data into it */
1616 l = (unsigned) r->ffmpeg.buf[c].length;
1617 if (r->ffmpeg.buf[c].memblock) {
1618 t = (int16_t*) ((uint8_t*) pa_memblock_acquire(r->ffmpeg.buf[c].memblock) + r->ffmpeg.buf[c].index);
1619 memcpy(p, t, l);
1620 pa_memblock_release(r->ffmpeg.buf[c].memblock);
1621 pa_memblock_unref(r->ffmpeg.buf[c].memblock);
1622 pa_memchunk_reset(&r->ffmpeg.buf[c]);
1623 }
1624
1625 /* Now append the new data, splitting up channels */
1626 t = ((int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index)) + c;
1627 k = (int16_t*) ((uint8_t*) p + l);
1628 for (u = 0; u < in_n_frames; u++) {
1629 *k = *t;
1630 t += r->o_ss.channels;
1631 k ++;
1632 }
1633 pa_memblock_release(input->memblock);
1634
1635 /* Calculate the resulting number of frames */
1636 in = (unsigned) in_n_frames + l / (unsigned) sizeof(int16_t);
1637
1638 /* Allocate buffer for the result */
1639 w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
1640 q = pa_memblock_acquire(w);
1641
1642 /* Now, resample */
1643 used_frames = (unsigned) av_resample(r->ffmpeg.state,
1644 q, p,
1645 &consumed_frames,
1646 (int) in, (int) *out_n_frames,
1647 c >= (unsigned) (r->o_ss.channels-1));
1648
1649 pa_memblock_release(b);
1650
1651 /* Now store the remaining samples away */
1652 pa_assert(consumed_frames <= (int) in);
1653 if (consumed_frames < (int) in) {
1654 r->ffmpeg.buf[c].memblock = b;
1655 r->ffmpeg.buf[c].index = (size_t) consumed_frames * sizeof(int16_t);
1656 r->ffmpeg.buf[c].length = (size_t) (in - (unsigned) consumed_frames) * sizeof(int16_t);
1657 } else
1658 pa_memblock_unref(b);
1659
1660 /* And place the results in the output buffer */
1661 s = (short*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index) + c;
1662 for (u = 0; u < used_frames; u++) {
1663 *s = *q;
1664 q++;
1665 s += r->o_ss.channels;
1666 }
1667 pa_memblock_release(output->memblock);
1668 pa_memblock_release(w);
1669 pa_memblock_unref(w);
1670 }
1671
1672 *out_n_frames = used_frames;
1673 }
1674
1675 static void ffmpeg_free(pa_resampler *r) {
1676 unsigned c;
1677
1678 pa_assert(r);
1679
1680 if (r->ffmpeg.state)
1681 av_resample_close(r->ffmpeg.state);
1682
1683 for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
1684 if (r->ffmpeg.buf[c].memblock)
1685 pa_memblock_unref(r->ffmpeg.buf[c].memblock);
1686 }
1687
1688 static int ffmpeg_init(pa_resampler *r) {
1689 unsigned c;
1690
1691 pa_assert(r);
1692
1693 /* We could probably implement different quality levels by
1694 * adjusting the filter parameters here. However, ffmpeg
1695 * internally only uses these hardcoded values, so let's use them
1696 * here for now as well until ffmpeg makes this configurable. */
1697
1698 if (!(r->ffmpeg.state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
1699 return -1;
1700
1701 r->impl_free = ffmpeg_free;
1702 r->impl_resample = ffmpeg_resample;
1703
1704 for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
1705 pa_memchunk_reset(&r->ffmpeg.buf[c]);
1706
1707 return 0;
1708 }
1709
1710 /*** copy (noop) implementation ***/
1711
1712 static int copy_init(pa_resampler *r) {
1713 pa_assert(r);
1714
1715 pa_assert(r->o_ss.rate == r->i_ss.rate);
1716
1717 return 0;
1718 }