summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/resampler.c
blob: eebc9c046b759244fc1497456f16697aaac56a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
/* $Id$ */

/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#if HAVE_LIBSAMPLERATE
#include <samplerate.h>
#endif

#include <liboil/liboilfuncs.h>
#include <liboil/liboil.h>

#include <pulse/xmalloc.h>
#include <pulsecore/sconv.h>
#include <pulsecore/log.h>
#include <pulsecore/macro.h>

#include "speexwrap.h"

#include "ffmpeg/avcodec.h"

#include "resampler.h"

/* Number of samples of extra space we allow the resamplers to return */
#define EXTRA_SAMPLES 128

struct pa_resampler {
    pa_resample_method_t resample_method;
    pa_sample_spec i_ss, o_ss;
    pa_channel_map i_cm, o_cm;
    size_t i_fz, o_fz, w_sz;
    pa_mempool *mempool;

    pa_memchunk buf1, buf2, buf3, buf4;
    unsigned buf1_samples, buf2_samples, buf3_samples, buf4_samples;

    pa_sample_format_t work_format;

    pa_convert_func_t to_work_format_func;
    pa_convert_func_t from_work_format_func;

    int map_table[PA_CHANNELS_MAX][PA_CHANNELS_MAX];
    int map_required;

    void (*impl_free)(pa_resampler *r);
    void (*impl_update_rates)(pa_resampler *r);
    void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);

    struct { /* data specific to the trivial resampler */
        unsigned o_counter;
        unsigned i_counter;
    } trivial;

#ifdef HAVE_LIBSAMPLERATE
    struct { /* data specific to libsamplerate */
        SRC_STATE *state;
    } src;
#endif

    struct { /* data specific to speex */
        SpeexResamplerState* state;
    } speex;

    struct { /* data specific to ffmpeg */
        struct AVResampleContext *state;
        pa_memchunk buf[PA_CHANNELS_MAX];
    } ffmpeg;
};

static int copy_init(pa_resampler *r);
static int trivial_init(pa_resampler*r);
static int speex_init(pa_resampler*r);
static int ffmpeg_init(pa_resampler*r);
#ifdef HAVE_LIBSAMPLERATE
static int libsamplerate_init(pa_resampler*r);
#endif

static void calc_map_table(pa_resampler *r);

static int (* const init_table[])(pa_resampler*r) = {
#ifdef HAVE_LIBSAMPLERATE
    [PA_RESAMPLER_SRC_SINC_BEST_QUALITY]   = libsamplerate_init,
    [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
    [PA_RESAMPLER_SRC_SINC_FASTEST]        = libsamplerate_init,
    [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD]     = libsamplerate_init,
    [PA_RESAMPLER_SRC_LINEAR]              = libsamplerate_init,
#else
    [PA_RESAMPLER_SRC_SINC_BEST_QUALITY]   = NULL,
    [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
    [PA_RESAMPLER_SRC_SINC_FASTEST]        = NULL,
    [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD]     = NULL,
    [PA_RESAMPLER_SRC_LINEAR]              = NULL,
#endif
    [PA_RESAMPLER_TRIVIAL]                 = trivial_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+0]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+1]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+2]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+3]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+4]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+5]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+6]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+7]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+8]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+9]      = speex_init,
    [PA_RESAMPLER_SPEEX_FLOAT_BASE+10]     = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+0]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+1]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+2]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+3]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+4]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+5]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+6]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+7]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+8]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+9]      = speex_init,
    [PA_RESAMPLER_SPEEX_FIXED_BASE+10]     = speex_init,
    [PA_RESAMPLER_FFMPEG]                  = ffmpeg_init,
    [PA_RESAMPLER_AUTO]                    = NULL,
    [PA_RESAMPLER_COPY]                    = copy_init
};

static inline size_t sample_size(pa_sample_format_t f) {
    pa_sample_spec ss = {
        .format = f,
        .rate = 0,
        .channels = 1
    };

    return pa_sample_size(&ss);
}

pa_resampler* pa_resampler_new(
        pa_mempool *pool,
        const pa_sample_spec *a,
        const pa_channel_map *am,
        const pa_sample_spec *b,
        const pa_channel_map *bm,
        pa_resample_method_t resample_method,
        int variable_rate) {

    pa_resampler *r = NULL;

    pa_assert(pool);
    pa_assert(a);
    pa_assert(b);
    pa_assert(pa_sample_spec_valid(a));
    pa_assert(pa_sample_spec_valid(b));
    pa_assert(resample_method >= 0);
    pa_assert(resample_method < PA_RESAMPLER_MAX);

    /* Fix method */

    if (!variable_rate && a->rate == b->rate) {
        pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
        resample_method = PA_RESAMPLER_COPY;
    }

    if (!pa_resample_method_supported(resample_method)) {
        pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(resample_method));
        resample_method = PA_RESAMPLER_AUTO;
    }

    if (resample_method == PA_RESAMPLER_FFMPEG && variable_rate) {
        pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
        resample_method = PA_RESAMPLER_AUTO;
    }

    if (resample_method == PA_RESAMPLER_COPY && (variable_rate || a->rate != b->rate)) {
        pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
        resample_method = PA_RESAMPLER_AUTO;
    }

    if (resample_method == PA_RESAMPLER_AUTO)
        resample_method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;

    r = pa_xnew(pa_resampler, 1);
    r->mempool = pool;
    r->resample_method = resample_method;

    r->impl_free = NULL;
    r->impl_update_rates = NULL;
    r->impl_resample = NULL;

    /* Fill sample specs */
    r->i_ss = *a;
    r->o_ss = *b;

    if (am)
        r->i_cm = *am;
    else
        pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT);

    if (bm)
        r->o_cm = *bm;
    else
        pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT);

    r->i_fz = pa_frame_size(a);
    r->o_fz = pa_frame_size(b);

    pa_memchunk_reset(&r->buf1);
    pa_memchunk_reset(&r->buf2);
    pa_memchunk_reset(&r->buf3);
    pa_memchunk_reset(&r->buf4);

    r->buf1_samples = r->buf2_samples = r->buf3_samples = r->buf4_samples = 0;

    calc_map_table(r);

    pa_log_info("Using resampler '%s'", pa_resample_method_to_string(resample_method));

    if ((resample_method >= PA_RESAMPLER_SPEEX_FIXED_BASE && resample_method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
        (resample_method == PA_RESAMPLER_FFMPEG))
        r->work_format = PA_SAMPLE_S16NE;
    else if (resample_method == PA_RESAMPLER_TRIVIAL || resample_method == PA_RESAMPLER_COPY) {

        if (r->map_required || a->format != b->format) {

            if (a->format == PA_SAMPLE_S32NE || a->format == PA_SAMPLE_S32RE ||
                a->format == PA_SAMPLE_FLOAT32NE || a->format == PA_SAMPLE_FLOAT32RE ||
                b->format == PA_SAMPLE_S32NE || b->format == PA_SAMPLE_S32RE ||
                b->format == PA_SAMPLE_FLOAT32NE || b->format == PA_SAMPLE_FLOAT32RE)
                r->work_format = PA_SAMPLE_FLOAT32NE;
            else
                r->work_format = PA_SAMPLE_S16NE;

        } else
            r->work_format = a->format;

    } else
        r->work_format = PA_SAMPLE_FLOAT32NE;

    pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));

    r->w_sz = sample_size(r->work_format);

    if (r->i_ss.format == r->work_format)
        r->to_work_format_func = NULL;
    else if (r->work_format == PA_SAMPLE_FLOAT32NE) {
        if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
            goto fail;
    } else {
        pa_assert(r->work_format == PA_SAMPLE_S16NE);
        if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
            goto fail;
    }

    if (r->o_ss.format == r->work_format)
        r->from_work_format_func = NULL;
    else if (r->work_format == PA_SAMPLE_FLOAT32NE) {
        if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
            goto fail;
    } else {
        pa_assert(r->work_format == PA_SAMPLE_S16NE);
        if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
            goto fail;
    }

    /* initialize implementation */
    if (init_table[resample_method](r) < 0)
        goto fail;

    return r;

fail:
    if (r)
        pa_xfree(r);

    return NULL;
}

void pa_resampler_free(pa_resampler *r) {
    pa_assert(r);

    if (r->impl_free)
        r->impl_free(r);

    if (r->buf1.memblock)
        pa_memblock_unref(r->buf1.memblock);
    if (r->buf2.memblock)
        pa_memblock_unref(r->buf2.memblock);
    if (r->buf3.memblock)
        pa_memblock_unref(r->buf3.memblock);
    if (r->buf4.memblock)
        pa_memblock_unref(r->buf4.memblock);

    pa_xfree(r);
}

void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
    pa_assert(r);
    pa_assert(rate > 0);

    if (r->i_ss.rate == rate)
        return;

    r->i_ss.rate = rate;

    r->impl_update_rates(r);
}

void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
    pa_assert(r);
    pa_assert(rate > 0);

    if (r->o_ss.rate == rate)
        return;

    r->o_ss.rate = rate;

    r->impl_update_rates(r);
}

size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
    pa_assert(r);

    return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
}

size_t pa_resampler_max_block_size(pa_resampler *r) {
    size_t block_size_max;
    pa_sample_spec ss;
    size_t fs;

    pa_assert(r);

    block_size_max = pa_mempool_block_size_max(r->mempool);

    /* We deduce the "largest" sample spec we're using during the
     * conversion */
    ss = r->i_ss;
    if (r->o_ss.channels > ss.channels)
        ss.channels = r->o_ss.channels;

    /* We silently assume that the format enum is ordered by size */
    if (r->o_ss.format > ss.format)
        ss.format = r->o_ss.format;
    if (r->work_format > ss.format)
        ss.format = r->work_format;

    if (r->o_ss.rate > ss.rate)
        ss.rate = r->o_ss.rate;

    fs = pa_frame_size(&ss);

    return (((block_size_max/fs + EXTRA_SAMPLES)*r->i_ss.rate)/ss.rate)*r->i_fz;
}

pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
    pa_assert(r);

    return r->resample_method;
}

static const char * const resample_methods[] = {
    "src-sinc-best-quality",
    "src-sinc-medium-quality",
    "src-sinc-fastest",
    "src-zero-order-hold",
    "src-linear",
    "trivial",
    "speex-float-0",
    "speex-float-1",
    "speex-float-2",
    "speex-float-3",
    "speex-float-4",
    "speex-float-5",
    "speex-float-6",
    "speex-float-7",
    "speex-float-8",
    "speex-float-9",
    "speex-float-10",
    "speex-fixed-0",
    "speex-fixed-1",
    "speex-fixed-2",
    "speex-fixed-3",
    "speex-fixed-4",
    "speex-fixed-5",
    "speex-fixed-6",
    "speex-fixed-7",
    "speex-fixed-8",
    "speex-fixed-9",
    "speex-fixed-10",
    "ffmpeg",
    "auto",
    "copy"
};

const char *pa_resample_method_to_string(pa_resample_method_t m) {

    if (m < 0 || m >= PA_RESAMPLER_MAX)
        return NULL;

    return resample_methods[m];
}

int pa_resample_method_supported(pa_resample_method_t m) {

    if (m < 0 || m >= PA_RESAMPLER_MAX)
        return 0;

#ifndef HAVE_LIBSAMPLERATE
    if (m <= PA_RESAMPLER_SRC_LINEAR)
        return 0;
#endif

    return 1;
}

pa_resample_method_t pa_parse_resample_method(const char *string) {
    pa_resample_method_t m;

    pa_assert(string);

    for (m = 0; m < PA_RESAMPLER_MAX; m++)
        if (!strcmp(string, resample_methods[m]))
            return m;

    if (!strcmp(string, "speex-fixed"))
        return PA_RESAMPLER_SPEEX_FIXED_BASE + 3;

    if (!strcmp(string, "speex-float"))
        return PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;

    return PA_RESAMPLER_INVALID;
}

static void calc_map_table(pa_resampler *r) {
    unsigned oc;

    pa_assert(r);

    if (!(r->map_required = (r->i_ss.channels != r->o_ss.channels || !pa_channel_map_equal(&r->i_cm, &r->o_cm))))
        return;

    for (oc = 0; oc < r->o_ss.channels; oc++) {
        unsigned ic, i = 0;

        for (ic = 0; ic < r->i_ss.channels; ic++) {
            pa_channel_position_t a, b;

            a = r->i_cm.map[ic];
            b = r->o_cm.map[oc];

            if (a == b ||
                (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_LEFT) ||
                (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_RIGHT) ||
                (a == PA_CHANNEL_POSITION_LEFT && b == PA_CHANNEL_POSITION_MONO) ||
                (a == PA_CHANNEL_POSITION_RIGHT && b == PA_CHANNEL_POSITION_MONO))

                r->map_table[oc][i++] = ic;
        }

        /* Add an end marker */
        if (i < PA_CHANNELS_MAX)
            r->map_table[oc][i] = -1;
    }
}

static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
    unsigned n_samples;
    void *src, *dst;

    pa_assert(r);
    pa_assert(input);
    pa_assert(input->memblock);

    /* Convert the incoming sample into the work sample format and place them in buf1 */

    if (!r->to_work_format_func || !input->length)
        return input;

    n_samples = (input->length / r->i_fz) * r->i_ss.channels;

    r->buf1.index = 0;
    r->buf1.length = r->w_sz * n_samples;

    if (!r->buf1.memblock || r->buf1_samples < n_samples) {
        if (r->buf1.memblock)
            pa_memblock_unref(r->buf1.memblock);

        r->buf1_samples = n_samples;
        r->buf1.memblock = pa_memblock_new(r->mempool, r->buf1.length);
    }

    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
    dst = (uint8_t*) pa_memblock_acquire(r->buf1.memblock);

    r->to_work_format_func(n_samples, src, dst);

    pa_memblock_release(input->memblock);
    pa_memblock_release(r->buf1.memblock);

    return &r->buf1;
}

static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
    unsigned in_n_samples, out_n_samples, n_frames;
    int i_skip, o_skip;
    unsigned oc;
    void *src, *dst;

    pa_assert(r);
    pa_assert(input);
    pa_assert(input->memblock);

    /* Remap channels and place the result int buf2 */

    if (!r->map_required || !input->length)
        return input;

    in_n_samples = input->length / r->w_sz;
    n_frames = in_n_samples / r->i_ss.channels;
    out_n_samples = n_frames * r->o_ss.channels;

    r->buf2.index = 0;
    r->buf2.length = r->w_sz * out_n_samples;

    if (!r->buf2.memblock || r->buf2_samples < out_n_samples) {
        if (r->buf2.memblock)
            pa_memblock_unref(r->buf2.memblock);

        r->buf2_samples = out_n_samples;
        r->buf2.memblock = pa_memblock_new(r->mempool, r->buf2.length);
    }

    src = ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
    dst = pa_memblock_acquire(r->buf2.memblock);

    memset(dst, 0, r->buf2.length);

    o_skip = r->w_sz * r->o_ss.channels;
    i_skip = r->w_sz * r->i_ss.channels;

    switch (r->work_format) {
        case PA_SAMPLE_FLOAT32NE:

            for (oc = 0; oc < r->o_ss.channels; oc++) {
                unsigned i;
                static const float one = 1.0;

                for (i = 0; i < PA_CHANNELS_MAX && r->map_table[oc][i] >= 0; i++)
                    oil_vectoradd_f32(
                            (float*) dst + oc, o_skip,
                            (float*) dst + oc, o_skip,
                            (float*) src + r->map_table[oc][i], i_skip,
                            n_frames,
                            &one, &one);
            }

            break;

        case PA_SAMPLE_S16NE:

            for (oc = 0; oc < r->o_ss.channels; oc++) {
                unsigned i;
                static const int16_t one = 1;

                for (i = 0; i < PA_CHANNELS_MAX && r->map_table[oc][i] >= 0; i++)
                    oil_vectoradd_s16(
                            (int16_t*) dst + oc, o_skip,
                            (int16_t*) dst + oc, o_skip,
                            (int16_t*) src + r->map_table[oc][i], i_skip,
                            n_frames,
                            &one, &one);
            }

            break;

        default:
            pa_assert_not_reached();
    }

    pa_memblock_release(input->memblock);
    pa_memblock_release(r->buf2.memblock);

    r->buf2.length = out_n_samples * r->w_sz;

    return &r->buf2;
}

static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
    unsigned in_n_frames, in_n_samples;
    unsigned out_n_frames, out_n_samples;

    pa_assert(r);
    pa_assert(input);

    /* Resample the data and place the result in buf3 */

    if (!r->impl_resample || !input->length)
        return input;

    in_n_samples = input->length / r->w_sz;
    in_n_frames = in_n_samples / r->o_ss.channels;

    out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_SAMPLES;
    out_n_samples = out_n_frames * r->o_ss.channels;

    r->buf3.index = 0;
    r->buf3.length = r->w_sz * out_n_samples;

    if (!r->buf3.memblock || r->buf3_samples < out_n_samples) {
        if (r->buf3.memblock)
            pa_memblock_unref(r->buf3.memblock);

        r->buf3_samples = out_n_samples;
        r->buf3.memblock = pa_memblock_new(r->mempool, r->buf3.length);
    }

    r->impl_resample(r, input, in_n_frames, &r->buf3, &out_n_frames);
    r->buf3.length = out_n_frames * r->w_sz * r->o_ss.channels;

    return &r->buf3;
}

static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
    unsigned n_samples, n_frames;
    void *src, *dst;

    pa_assert(r);
    pa_assert(input);

    /* Convert the data into the correct sample type and place the result in buf4 */

    if (!r->from_work_format_func || !input->length)
        return input;

    n_samples = input->length / r->w_sz;
    n_frames =  n_samples / r->o_ss.channels;

    r->buf4.index = 0;
    r->buf4.length = r->o_fz * n_frames;

    if (!r->buf4.memblock || r->buf4_samples < n_samples) {
        if (r->buf4.memblock)
            pa_memblock_unref(r->buf4.memblock);

        r->buf4_samples = n_samples;
        r->buf4.memblock = pa_memblock_new(r->mempool, r->buf4.length);
    }

    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
    dst = pa_memblock_acquire(r->buf4.memblock);
    r->from_work_format_func(n_samples, src, dst);
    pa_memblock_release(input->memblock);
    pa_memblock_release(r->buf4.memblock);

    r->buf4.length = r->o_fz * n_frames;

    return &r->buf4;
}

void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
    pa_memchunk *buf;

    pa_assert(r);
    pa_assert(in);
    pa_assert(out);
    pa_assert(in->length);
    pa_assert(in->memblock);
    pa_assert(in->length % r->i_fz == 0);

    buf = (pa_memchunk*) in;
    buf = convert_to_work_format(r, buf);
    buf = remap_channels(r, buf);
    buf = resample(r, buf);

    if (buf->length) {
        buf = convert_from_work_format(r, buf);
        *out = *buf;

        if (buf == in)
            pa_memblock_ref(buf->memblock);
        else
            pa_memchunk_reset(buf);
    } else
        pa_memchunk_reset(out);
}

/*** libsamplerate based implementation ***/

#ifdef HAVE_LIBSAMPLERATE
static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
    SRC_DATA data;

    pa_assert(r);
    pa_assert(input);
    pa_assert(output);
    pa_assert(out_n_frames);

    memset(&data, 0, sizeof(data));

    data.data_in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
    data.input_frames = in_n_frames;

    data.data_out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
    data.output_frames = *out_n_frames;

    data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
    data.end_of_input = 0;

    pa_assert_se(src_process(r->src.state, &data) == 0);
    pa_assert((unsigned) data.input_frames_used == in_n_frames);

    pa_memblock_release(input->memblock);
    pa_memblock_release(output->memblock);

    *out_n_frames = data.output_frames_gen;
}

static void libsamplerate_update_rates(pa_resampler *r) {
    pa_assert(r);

    pa_assert_se(src_set_ratio(r->src.state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
}

static void libsamplerate_free(pa_resampler *r) {
    pa_assert(r);

    if (r->src.state)
        src_delete(r->src.state);
}

static int libsamplerate_init(pa_resampler *r) {
    int err;

    pa_assert(r);

    if (!(r->src.state = src_new(r->resample_method, r->o_ss.channels, &err)))
        return -1;

    r->impl_free = libsamplerate_free;
    r->impl_update_rates = libsamplerate_update_rates;
    r->impl_resample = libsamplerate_resample;

    return 0;
}
#endif

/*** speex based implementation ***/

static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
    float *in, *out;
    uint32_t inf = in_n_frames, outf = *out_n_frames;

    pa_assert(r);
    pa_assert(input);
    pa_assert(output);
    pa_assert(out_n_frames);

    in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
    out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);

    pa_assert_se(paspfl_resampler_process_interleaved_float(r->speex.state, in, &inf, out, &outf) == 0);

    pa_memblock_release(input->memblock);
    pa_memblock_release(output->memblock);

    pa_assert(inf == in_n_frames);
    *out_n_frames = outf;
}

static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
    int16_t *in, *out;
    uint32_t inf = in_n_frames, outf = *out_n_frames;

    pa_assert(r);
    pa_assert(input);
    pa_assert(output);
    pa_assert(out_n_frames);

    in = (int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
    out = (int16_t*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);

    pa_assert_se(paspfx_resampler_process_interleaved_int(r->speex.state, in, &inf, out, &outf) == 0);

    pa_memblock_release(input->memblock);
    pa_memblock_release(output->memblock);

    pa_assert(inf == in_n_frames);
    *out_n_frames = outf;
}

static void speex_update_rates(pa_resampler *r) {
    pa_assert(r);

    if (r->resample_method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->resample_method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
        pa_assert_se(paspfx_resampler_set_rate(r->speex.state, r->i_ss.rate, r->o_ss.rate) == 0);
    else {
        pa_assert(r->resample_method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->resample_method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
        pa_assert_se(paspfl_resampler_set_rate(r->speex.state, r->i_ss.rate, r->o_ss.rate) == 0);
    }
}

static void speex_free(pa_resampler *r) {
    pa_assert(r);

    if (!r->speex.state)
        return;

    if (r->resample_method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->resample_method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
        paspfx_resampler_destroy(r->speex.state);
    else {
        pa_assert(r->resample_method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->resample_method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
        paspfl_resampler_destroy(r->speex.state);
    }
}

static int speex_init(pa_resampler *r) {
    int q, err;

    pa_assert(r);

    r->impl_free = speex_free;
    r->impl_update_rates = speex_update_rates;

    if (r->resample_method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->resample_method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
        q = r->resample_method - PA_RESAMPLER_SPEEX_FIXED_BASE;

        pa_log_info("Choosing speex quality setting %i.", q);

        if (!(r->speex.state = paspfx_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
            return -1;

        r->impl_resample = speex_resample_int;
    } else {
        pa_assert(r->resample_method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->resample_method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
        q = r->resample_method - PA_RESAMPLER_SPEEX_FLOAT_BASE;

        pa_log_info("Choosing speex quality setting %i.", q);

        if (!(r->speex.state = paspfl_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
            return -1;

        r->impl_resample = speex_resample_float;
    }

    return 0;
}

/* Trivial implementation */

static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
    size_t fz;
    unsigned o_index;
    void *src, *dst;

    pa_assert(r);
    pa_assert(input);
    pa_assert(output);
    pa_assert(out_n_frames);

    fz = r->w_sz * r->o_ss.channels;

    src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
    dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;

    for (o_index = 0;; o_index++, r->trivial.o_counter++) {
        unsigned j;

        j = ((r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate);
        j = j > r->trivial.i_counter ? j - r->trivial.i_counter : 0;

        if (j >= in_n_frames)
            break;

        pa_assert(o_index * fz < pa_memblock_get_length(output->memblock));

        oil_memcpy((uint8_t*) dst + fz * o_index,
                   (uint8_t*) src + fz * j, fz);
    }

    pa_memblock_release(input->memblock);
    pa_memblock_release(output->memblock);

    *out_n_frames = o_index;

    r->trivial.i_counter += in_n_frames;

    /* Normalize counters */
    while (r->trivial.i_counter >= r->i_ss.rate) {
        pa_assert(r->trivial.o_counter >= r->o_ss.rate);

        r->trivial.i_counter -= r->i_ss.rate;
        r->trivial.o_counter -= r->o_ss.rate;
    }
}

static void trivial_update_rates(pa_resampler *r) {
    pa_assert(r);

    r->trivial.i_counter = 0;
    r->trivial.o_counter = 0;
}

static int trivial_init(pa_resampler*r) {
    pa_assert(r);

    r->trivial.o_counter = r->trivial.i_counter = 0;

    r->impl_resample = trivial_resample;
    r->impl_update_rates = trivial_update_rates;
    r->impl_free = NULL;

    return 0;
}

/*** ffmpeg based implementation ***/

static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
    unsigned used_frames = 0, c;

    pa_assert(r);
    pa_assert(input);
    pa_assert(output);
    pa_assert(out_n_frames);

    for (c = 0; c < r->o_ss.channels; c++) {
        unsigned u;
        pa_memblock *b, *w;
        int16_t *p, *t, *k, *q, *s;
        int consumed_frames;
        unsigned in, l;

        /* Allocate a new block */
        b = pa_memblock_new(r->mempool, r->ffmpeg.buf[c].length + in_n_frames * sizeof(int16_t));
        p = pa_memblock_acquire(b);

        /* Copy the remaining data into it */
        l = r->ffmpeg.buf[c].length;
        if (r->ffmpeg.buf[c].memblock) {
            t = (int16_t*) ((uint8_t*) pa_memblock_acquire(r->ffmpeg.buf[c].memblock) + r->ffmpeg.buf[c].index);
            memcpy(p, t, l);
            pa_memblock_release(r->ffmpeg.buf[c].memblock);
            pa_memblock_unref(r->ffmpeg.buf[c].memblock);
            pa_memchunk_reset(&r->ffmpeg.buf[c]);
        }

        /* Now append the new data, splitting up channels */
        t = ((int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index)) + c;
        k = (int16_t*) ((uint8_t*) p + l);
        for (u = 0; u < in_n_frames; u++) {
            *k = *t;
            t += r->o_ss.channels;
            k ++;
        }
        pa_memblock_release(input->memblock);

        /* Calculate the resulting number of frames */
        in = in_n_frames + l / sizeof(int16_t);

        /* Allocate buffer for the result */
        w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
        q = pa_memblock_acquire(w);

        /* Now, resample */
        used_frames = av_resample(r->ffmpeg.state,
                                  q, p,
                                  &consumed_frames,
                                  in, *out_n_frames,
                                  c >= (unsigned) r->o_ss.channels-1);

        pa_memblock_release(b);

        /* Now store the remaining samples away */
        pa_assert(consumed_frames <= (int) in);
        if (consumed_frames < (int) in) {
            r->ffmpeg.buf[c].memblock = b;
            r->ffmpeg.buf[c].index = consumed_frames * sizeof(int16_t);
            r->ffmpeg.buf[c].length = (in - consumed_frames) * sizeof(int16_t);
        } else
            pa_memblock_unref(b);

        /* And place the results in the output buffer */
        s = (short*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index) + c;
        for (u = 0; u < used_frames; u++) {
            *s = *q;
            q++;
            s += r->o_ss.channels;
        }
        pa_memblock_release(output->memblock);
        pa_memblock_release(w);
        pa_memblock_unref(w);
    }

    *out_n_frames = used_frames;
}

static void ffmpeg_free(pa_resampler *r) {
    unsigned c;

    pa_assert(r);

    if (r->ffmpeg.state)
        av_resample_close(r->ffmpeg.state);

    for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
        if (r->ffmpeg.buf[c].memblock)
            pa_memblock_unref(r->ffmpeg.buf[c].memblock);
}

static int ffmpeg_init(pa_resampler *r) {
    unsigned c;

    pa_assert(r);

    /* We could probably implement different quality levels by
     * adjusting the filter parameters here. However, ffmpeg
     * internally only uses these hardcoded values, so let's use them
     * here for now as well until ffmpeg makes this configurable. */

    if (!(r->ffmpeg.state = av_resample_init(r->o_ss.rate, r->i_ss.rate, 16, 10, 0, 0.8)))
        return -1;

    r->impl_free = ffmpeg_free;
    r->impl_resample = ffmpeg_resample;

    for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
        pa_memchunk_reset(&r->ffmpeg.buf[c]);

    return 0;
}

/*** copy (noop) implementation ***/

static int copy_init(pa_resampler *r) {
    pa_assert(r);

    pa_assert(r->o_ss.rate == r->i_ss.rate);

    r->impl_free = NULL;
    r->impl_resample = NULL;
    r->impl_update_rates = NULL;

    return 0;
}