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