Browse code

polyphase kaiser windowed sinc and blackman nuttall windowed sinc audio resample filters

Originally committed as revision 3228 to svn://svn.ffmpeg.org/ffmpeg/trunk

Michael Niedermayer authored on 2004/06/18 00:43:23
Showing 4 changed files
... ...
@@ -1846,6 +1846,7 @@ extern AVCodec ac3_decoder;
1846 1846
 /* resample.c */
1847 1847
 
1848 1848
 struct ReSampleContext;
1849
+struct AVResampleContext;
1849 1850
 
1850 1851
 typedef struct ReSampleContext ReSampleContext;
1851 1852
 
... ...
@@ -1854,6 +1855,9 @@ ReSampleContext *audio_resample_init(int output_channels, int input_channels,
1854 1854
 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples);
1855 1855
 void audio_resample_close(ReSampleContext *s);
1856 1856
 
1857
+struct AVResampleContext *av_resample_init(int out_rate, int in_rate);
1858
+int av_resample(struct AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx);
1859
+
1857 1860
 /* YUV420 format is assumed ! */
1858 1861
 
1859 1862
 struct ImgReSampleContext;
... ...
@@ -55,6 +55,8 @@ struct ImgReSampleContext {
55 55
     uint8_t *line_buf;
56 56
 };
57 57
 
58
+void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_count, int scale, int type);
59
+
58 60
 static inline int get_phase(int pos)
59 61
 {
60 62
     return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1);
... ...
@@ -540,48 +542,6 @@ static void component_resample(ImgReSampleContext *s,
540 540
     }
541 541
 }
542 542
 
543
-/* XXX: the following filter is quite naive, but it seems to suffice
544
-   for 4 taps */
545
-static void build_filter(int16_t *filter, float factor)
546
-{
547
-    int ph, i, v;
548
-    float x, y, tab[NB_TAPS], norm, mult, target;
549
-
550
-    /* if upsampling, only need to interpolate, no filter */
551
-    if (factor > 1.0)
552
-        factor = 1.0;
553
-
554
-    for(ph=0;ph<NB_PHASES;ph++) {
555
-        norm = 0;
556
-        for(i=0;i<NB_TAPS;i++) {
557
-#if 1
558
-            const float d= -0.5; //first order derivative = -0.5
559
-            x = fabs(((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor);
560
-            if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*(            -x*x + x*x*x);
561
-            else      y=                       d*(-4 + 8*x - 5*x*x + x*x*x);
562
-#else
563
-            x = M_PI * ((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor;
564
-            if (x == 0)
565
-                y = 1.0;
566
-            else
567
-                y = sin(x) / x;
568
-#endif
569
-            tab[i] = y;
570
-            norm += y;
571
-        }
572
-
573
-        /* normalize so that an uniform color remains the same */
574
-        target= 1 << FILTER_BITS;
575
-        for(i=0;i<NB_TAPS;i++) {
576
-            mult = target / norm;
577
-            v = lrintf(tab[i] * mult);
578
-            filter[ph * NB_TAPS + i] = v;
579
-            norm -= tab[i];
580
-            target -= v;
581
-        }
582
-    }
583
-}
584
-
585 543
 ImgReSampleContext *img_resample_init(int owidth, int oheight,
586 544
                                       int iwidth, int iheight)
587 545
 {
... ...
@@ -626,10 +586,10 @@ ImgReSampleContext *img_resample_full_init(int owidth, int oheight,
626 626
     s->h_incr = ((iwidth - leftBand - rightBand) * POS_FRAC) / s->pad_owidth;
627 627
     s->v_incr = ((iheight - topBand - bottomBand) * POS_FRAC) / s->pad_oheight; 
628 628
 
629
-    build_filter(&s->h_filters[0][0], (float) s->pad_owidth  / 
630
-            (float) (iwidth - leftBand - rightBand));
631
-    build_filter(&s->v_filters[0][0], (float) s->pad_oheight / 
632
-            (float) (iheight - topBand - bottomBand));
629
+    av_build_filter(&s->h_filters[0][0], (float) s->pad_owidth  / 
630
+            (float) (iwidth - leftBand - rightBand), NB_TAPS, NB_PHASES, 1<<FILTER_BITS, 0);
631
+    av_build_filter(&s->v_filters[0][0], (float) s->pad_oheight / 
632
+            (float) (iheight - topBand - bottomBand), NB_TAPS, NB_PHASES, 1<<FILTER_BITS, 0);
633 633
 
634 634
     return s;
635 635
 fail:
... ...
@@ -24,103 +24,17 @@
24 24
 
25 25
 #include "avcodec.h"
26 26
 
27
-typedef struct {
28
-    /* fractional resampling */
29
-    uint32_t incr; /* fractional increment */
30
-    uint32_t frac;
31
-    int last_sample;
32
-    /* integer down sample */
33
-    int iratio;  /* integer divison ratio */
34
-    int icount, isum;
35
-    int inv;
36
-} ReSampleChannelContext;
27
+struct AVResampleContext;
37 28
 
38 29
 struct ReSampleContext {
39
-    ReSampleChannelContext channel_ctx[2];
30
+    struct AVResampleContext *resample_context;
31
+    short *temp[2];
32
+    int temp_len;
40 33
     float ratio;
41 34
     /* channel convert */
42 35
     int input_channels, output_channels, filter_channels;
43 36
 };
44 37
 
45
-
46
-#define FRAC_BITS 16
47
-#define FRAC (1 << FRAC_BITS)
48
-
49
-static void init_mono_resample(ReSampleChannelContext *s, float ratio)
50
-{
51
-    ratio = 1.0 / ratio;
52
-    s->iratio = (int)floorf(ratio);
53
-    if (s->iratio == 0)
54
-        s->iratio = 1;
55
-    s->incr = (int)((ratio / s->iratio) * FRAC);
56
-    s->frac = FRAC;
57
-    s->last_sample = 0;
58
-    s->icount = s->iratio;
59
-    s->isum = 0;
60
-    s->inv = (FRAC / s->iratio);
61
-}
62
-
63
-/* fractional audio resampling */
64
-static int fractional_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
65
-{
66
-    unsigned int frac, incr;
67
-    int l0, l1;
68
-    short *q, *p, *pend;
69
-
70
-    l0 = s->last_sample;
71
-    incr = s->incr;
72
-    frac = s->frac;
73
-
74
-    p = input;
75
-    pend = input + nb_samples;
76
-    q = output;
77
-
78
-    l1 = *p++;
79
-    for(;;) {
80
-        /* interpolate */
81
-        *q++ = (l0 * (FRAC - frac) + l1 * frac) >> FRAC_BITS;
82
-        frac = frac + s->incr;
83
-        while (frac >= FRAC) {
84
-            frac -= FRAC;
85
-            if (p >= pend)
86
-                goto the_end;
87
-            l0 = l1;
88
-            l1 = *p++;
89
-        }
90
-    }
91
- the_end:
92
-    s->last_sample = l1;
93
-    s->frac = frac;
94
-    return q - output;
95
-}
96
-
97
-static int integer_downsample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
98
-{
99
-    short *q, *p, *pend;
100
-    int c, sum;
101
-
102
-    p = input;
103
-    pend = input + nb_samples;
104
-    q = output;
105
-
106
-    c = s->icount;
107
-    sum = s->isum;
108
-
109
-    for(;;) {
110
-        sum += *p++;
111
-        if (--c == 0) {
112
-            *q++ = (sum * s->inv) >> FRAC_BITS;
113
-            c = s->iratio;
114
-            sum = 0;
115
-        }
116
-        if (p >= pend)
117
-            break;
118
-    }
119
-    s->isum = sum;
120
-    s->icount = c;
121
-    return q - output;
122
-}
123
-
124 38
 /* n1: number of samples */
125 39
 static void stereo_to_mono(short *output, short *input, int n1)
126 40
 {
... ...
@@ -210,31 +124,6 @@ static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
210 210
     }
211 211
 }
212 212
 
213
-static int mono_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
214
-{
215
-    short *buf1;
216
-    short *buftmp;
217
-
218
-    buf1= (short*)av_malloc( nb_samples * sizeof(short) );
219
-
220
-    /* first downsample by an integer factor with averaging filter */
221
-    if (s->iratio > 1) {
222
-        buftmp = buf1;
223
-        nb_samples = integer_downsample(s, buftmp, input, nb_samples);
224
-    } else {
225
-        buftmp = input;
226
-    }
227
-
228
-    /* then do a fractional resampling with linear interpolation */
229
-    if (s->incr != FRAC) {
230
-        nb_samples = fractional_resample(s, output, buftmp, nb_samples);
231
-    } else {
232
-        memcpy(output, buftmp, nb_samples * sizeof(short));
233
-    }
234
-    av_free(buf1);
235
-    return nb_samples;
236
-}
237
-
238 213
 ReSampleContext *audio_resample_init(int output_channels, int input_channels, 
239 214
                                       int output_rate, int input_rate)
240 215
 {
... ...
@@ -271,16 +160,13 @@ ReSampleContext *audio_resample_init(int output_channels, int input_channels,
271 271
     if(s->filter_channels>2)
272 272
       s->filter_channels = 2;
273 273
 
274
-    for(i=0;i<s->filter_channels;i++) {
275
-        init_mono_resample(&s->channel_ctx[i], s->ratio);
276
-    }
274
+    s->resample_context= av_resample_init(output_rate, input_rate);
275
+    
277 276
     return s;
278 277
 }
279 278
 
280 279
 /* resample audio. 'nb_samples' is the number of input samples */
281 280
 /* XXX: optimize it ! */
282
-/* XXX: do it with polyphase filters, since the quality here is
283
-   HORRIBLE. Return the number of samples available in output */
284 281
 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
285 282
 {
286 283
     int i, nb_samples1;
... ...
@@ -296,8 +182,11 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
296 296
     }
297 297
 
298 298
     /* XXX: move those malloc to resample init code */
299
-    bufin[0]= (short*) av_malloc( nb_samples * sizeof(short) );
300
-    bufin[1]= (short*) av_malloc( nb_samples * sizeof(short) );
299
+    for(i=0; i<s->filter_channels; i++){
300
+        bufin[i]= (short*) av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
301
+        memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
302
+        buftmp2[i] = bufin[i] + s->temp_len;
303
+    }
301 304
     
302 305
     /* make some zoom to avoid round pb */
303 306
     lenout= (int)(nb_samples * s->ratio) + 16;
... ...
@@ -306,27 +195,32 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
306 306
 
307 307
     if (s->input_channels == 2 &&
308 308
         s->output_channels == 1) {
309
-        buftmp2[0] = bufin[0];
310 309
         buftmp3[0] = output;
311 310
         stereo_to_mono(buftmp2[0], input, nb_samples);
312 311
     } else if (s->output_channels >= 2 && s->input_channels == 1) {
313
-        buftmp2[0] = input;
314 312
         buftmp3[0] = bufout[0];
313
+        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
315 314
     } else if (s->output_channels >= 2) {
316
-        buftmp2[0] = bufin[0];
317
-        buftmp2[1] = bufin[1];
318 315
         buftmp3[0] = bufout[0];
319 316
         buftmp3[1] = bufout[1];
320 317
         stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
321 318
     } else {
322
-        buftmp2[0] = input;
323 319
         buftmp3[0] = output;
320
+        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
324 321
     }
325 322
 
323
+    nb_samples += s->temp_len;
324
+
326 325
     /* resample each channel */
327 326
     nb_samples1 = 0; /* avoid warning */
328 327
     for(i=0;i<s->filter_channels;i++) {
329
-        nb_samples1 = mono_resample(&s->channel_ctx[i], buftmp3[i], buftmp2[i], nb_samples);
328
+        int consumed;
329
+        int is_last= i+1 == s->filter_channels;
330
+
331
+        nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
332
+        s->temp_len= nb_samples - consumed;
333
+        s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
334
+        memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
330 335
     }
331 336
 
332 337
     if (s->output_channels == 2 && s->input_channels == 1) {
... ...
@@ -347,5 +241,8 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
347 347
 
348 348
 void audio_resample_close(ReSampleContext *s)
349 349
 {
350
+    av_resample_close(s->resample_context);
351
+    av_freep(&s->temp[0]);
352
+    av_freep(&s->temp[1]);
350 353
     av_free(s);
351 354
 }
352 355
new file mode 100644
... ...
@@ -0,0 +1,214 @@
0
+/*
1
+ * audio resampling
2
+ * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
+ *
18
+ */
19
+ 
20
+/**
21
+ * @file resample2.c
22
+ * audio resampling
23
+ * @author Michael Niedermayer <michaelni@gmx.at>
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "common.h"
28
+
29
+#define PHASE_SHIFT 10
30
+#define PHASE_COUNT (1<<PHASE_SHIFT)
31
+#define PHASE_MASK (PHASE_COUNT-1)
32
+#define FILTER_SHIFT 15
33
+
34
+typedef struct AVResampleContext{
35
+    short *filter_bank;
36
+    int filter_length;
37
+    int ideal_dst_incr;
38
+    int dst_incr;
39
+    int index;
40
+    int frac;
41
+    int src_incr;
42
+    int compensation_distance;
43
+}AVResampleContext;
44
+
45
+/**
46
+ * 0th order modified bessel function of the first kind.
47
+ */
48
+double bessel(double x){
49
+    double v=1;
50
+    double t=1;
51
+    int i;
52
+    
53
+    for(i=1; i<50; i++){
54
+        t *= i;
55
+        v += pow(x*x/4, i)/(t*t);
56
+    }
57
+    return v;
58
+}
59
+
60
+/**
61
+ * builds a polyphase filterbank.
62
+ * @param factor resampling factor
63
+ * @param scale wanted sum of coefficients for each filter
64
+ * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2->kaiser windowed sinc beta=16
65
+ */
66
+void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_count, int scale, int type){
67
+    int ph, i, v;
68
+    double x, y, w, tab[tap_count];
69
+    const int center= (tap_count-1)/2;
70
+
71
+    /* if upsampling, only need to interpolate, no filter */
72
+    if (factor > 1.0)
73
+        factor = 1.0;
74
+
75
+    for(ph=0;ph<phase_count;ph++) {
76
+        double norm = 0;
77
+        double e= 0;
78
+        for(i=0;i<tap_count;i++) {
79
+            x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
80
+            if (x == 0) y = 1.0;
81
+            else        y = sin(x) / x;
82
+            switch(type){
83
+            case 0:{
84
+                const float d= -0.5; //first order derivative = -0.5
85
+                x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
86
+                if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*(            -x*x + x*x*x);
87
+                else      y=                       d*(-4 + 8*x - 5*x*x + x*x*x);
88
+                break;}
89
+            case 1:
90
+                w = 2.0*x / (factor*tap_count) + M_PI;
91
+                y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
92
+                break;
93
+            case 2:
94
+                w = 2.0*x / (factor*tap_count*M_PI);
95
+                y *= bessel(16*sqrt(FFMAX(1-w*w, 0))) / bessel(16);
96
+                break;
97
+            }
98
+
99
+            tab[i] = y;
100
+            norm += y;
101
+        }
102
+
103
+        /* normalize so that an uniform color remains the same */
104
+        for(i=0;i<tap_count;i++) {
105
+            v = clip(lrintf(tab[i] * scale / norm) + e, -32768, 32767);
106
+            filter[ph * tap_count + i] = v;
107
+            e += tab[i] * scale / norm - v;
108
+        }
109
+    }
110
+}
111
+
112
+/**
113
+ * initalizes a audio resampler.
114
+ * note, if either rate is not a integer then simply scale both rates up so they are
115
+ */
116
+AVResampleContext *av_resample_init(int out_rate, int in_rate){
117
+    AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
118
+    double factor= FFMIN(out_rate / (double)in_rate, 1.0);
119
+
120
+    memset(c, 0, sizeof(AVResampleContext));
121
+
122
+    c->filter_length= ceil(16.0/factor);
123
+    c->filter_bank= av_mallocz(c->filter_length*(PHASE_COUNT+1)*sizeof(short));
124
+    av_build_filter(c->filter_bank, factor, c->filter_length, PHASE_COUNT, 1<<FILTER_SHIFT, 1);
125
+    c->filter_bank[c->filter_length*PHASE_COUNT + (c->filter_length-1) + 1]= (1<<FILTER_SHIFT)-1;
126
+    c->filter_bank[c->filter_length*PHASE_COUNT + (c->filter_length-1) + 2]= 1;
127
+
128
+    c->src_incr= out_rate;
129
+    c->ideal_dst_incr= c->dst_incr= in_rate * PHASE_COUNT;
130
+    c->index= -PHASE_COUNT*((c->filter_length-1)/2);
131
+
132
+    return c;
133
+}
134
+
135
+void av_resample_close(AVResampleContext *c){
136
+    av_freep(&c->filter_bank);
137
+    av_freep(&c);
138
+}
139
+
140
+void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
141
+    assert(!c->compensation_distance); //FIXME
142
+
143
+    c->compensation_distance= compensation_distance;
144
+    c->dst_incr-= c->ideal_dst_incr * sample_delta / compensation_distance;
145
+}
146
+
147
+/**
148
+ * resamples.
149
+ * @param src an array of unconsumed samples
150
+ * @param consumed the number of samples of src which have been consumed are returned here
151
+ * @param src_size the number of unconsumed samples available
152
+ * @param dst_size the amount of space in samples available in dst
153
+ * @param update_ctx if this is 0 then the context wont be modified, that way several channels can be resampled with the same context
154
+ * @return the number of samples written in dst or -1 if an error occured
155
+ */
156
+int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
157
+    int dst_index, i;
158
+    int index= c->index;
159
+    int frac= c->frac;
160
+    int dst_incr_frac= c->dst_incr % c->src_incr;
161
+    int dst_incr=      c->dst_incr / c->src_incr;
162
+    
163
+    if(c->compensation_distance && c->compensation_distance < dst_size)
164
+        dst_size= c->compensation_distance;
165
+    
166
+    for(dst_index=0; dst_index < dst_size; dst_index++){
167
+        short *filter= c->filter_bank + c->filter_length*(index & PHASE_MASK);
168
+        int sample_index= index >> PHASE_SHIFT;
169
+        int val=0;
170
+        
171
+        if(sample_index < 0){
172
+            for(i=0; i<c->filter_length; i++)
173
+                val += src[ABS(sample_index + i)] * filter[i];
174
+        }else if(sample_index + c->filter_length > src_size){
175
+            break;
176
+        }else{
177
+#if 0
178
+            int64_t v=0;
179
+            int sub_phase= (frac<<12) / c->src_incr;
180
+            for(i=0; i<c->filter_length; i++){
181
+                int64_t coeff= filter[i]*(4096 - sub_phase) + filter[i + c->filter_length]*sub_phase;
182
+                v += src[sample_index + i] * coeff;
183
+            }
184
+            val= v>>12;
185
+#else
186
+            for(i=0; i<c->filter_length; i++){
187
+                val += src[sample_index + i] * filter[i];
188
+            }
189
+#endif
190
+        }
191
+
192
+        val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
193
+        dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
194
+
195
+        frac += dst_incr_frac;
196
+        index += dst_incr;
197
+        if(frac >= c->src_incr){
198
+            frac -= c->src_incr;
199
+            index++;
200
+        }
201
+    }
202
+    if(update_ctx){
203
+        if(c->compensation_distance){
204
+            c->compensation_distance -= index;
205
+            if(!c->compensation_distance)
206
+                c->dst_incr= c->ideal_dst_incr;
207
+        }
208
+        c->frac= frac;
209
+        c->index=0;
210
+    }
211
+    *consumed= index >> PHASE_SHIFT;
212
+    return dst_index;
213
+}