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