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