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