Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mdct.c @ 4eb7a735

History | View | Annotate | Download (5.01 KB)

1
/*
2
 * MDCT/IMDCT transforms
3
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "dsputil.h"
22

    
23
/**
24
 * @file mdct.c
25
 * MDCT/IMDCT transforms.
26
 */
27

    
28
// Generate a Kaiser-Bessel Derived Window.
29
void ff_kbd_window_init(float *window)
30
{
31
   int i, j;
32
   double sum = 0.0, bessel, tmp;
33
   double local_window[256];
34
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
35

    
36
   for (i = 0; i < 256; i++) {
37
       tmp = i * (256 - i) * alpha2;
38
       bessel = 1.0;
39
       for (j = 100; j > 0; j--) /* default to 100 iterations */
40
           bessel = bessel * tmp / (j * j) + 1;
41
       sum += bessel;
42
       local_window[i] = sum;
43
   }
44

    
45
   sum++;
46
   for (i = 0; i < 256; i++)
47
       window[i] = sqrt(local_window[i] / sum);
48
}
49

    
50
/**
51
 * init MDCT or IMDCT computation.
52
 */
53
int ff_mdct_init(MDCTContext *s, int nbits, int inverse)
54
{
55
    int n, n4, i;
56
    float alpha;
57

    
58
    memset(s, 0, sizeof(*s));
59
    n = 1 << nbits;
60
    s->nbits = nbits;
61
    s->n = n;
62
    n4 = n >> 2;
63
    s->tcos = av_malloc(n4 * sizeof(FFTSample));
64
    if (!s->tcos)
65
        goto fail;
66
    s->tsin = av_malloc(n4 * sizeof(FFTSample));
67
    if (!s->tsin)
68
        goto fail;
69

    
70
    for(i=0;i<n4;i++) {
71
        alpha = 2 * M_PI * (i + 1.0 / 8.0) / n;
72
        s->tcos[i] = -cos(alpha);
73
        s->tsin[i] = -sin(alpha);
74
    }
75
    if (ff_fft_init(&s->fft, s->nbits - 2, inverse) < 0)
76
        goto fail;
77
    return 0;
78
 fail:
79
    av_freep(&s->tcos);
80
    av_freep(&s->tsin);
81
    return -1;
82
}
83

    
84
/* complex multiplication: p = a * b */
85
#define CMUL(pre, pim, are, aim, bre, bim) \
86
{\
87
    float _are = (are);\
88
    float _aim = (aim);\
89
    float _bre = (bre);\
90
    float _bim = (bim);\
91
    (pre) = _are * _bre - _aim * _bim;\
92
    (pim) = _are * _bim + _aim * _bre;\
93
}
94

    
95
/**
96
 * Compute inverse MDCT of size N = 2^nbits
97
 * @param output N samples
98
 * @param input N/2 samples
99
 * @param tmp N/2 samples
100
 */
101
void ff_imdct_calc(MDCTContext *s, FFTSample *output,
102
                   const FFTSample *input, FFTSample *tmp)
103
{
104
    int k, n8, n4, n2, n, j;
105
    const uint16_t *revtab = s->fft.revtab;
106
    const FFTSample *tcos = s->tcos;
107
    const FFTSample *tsin = s->tsin;
108
    const FFTSample *in1, *in2;
109
    FFTComplex *z = (FFTComplex *)tmp;
110

    
111
    n = 1 << s->nbits;
112
    n2 = n >> 1;
113
    n4 = n >> 2;
114
    n8 = n >> 3;
115

    
116
    /* pre rotation */
117
    in1 = input;
118
    in2 = input + n2 - 1;
119
    for(k = 0; k < n4; k++) {
120
        j=revtab[k];
121
        CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
122
        in1 += 2;
123
        in2 -= 2;
124
    }
125
    ff_fft_calc(&s->fft, z);
126

    
127
    /* post rotation + reordering */
128
    /* XXX: optimize */
129
    for(k = 0; k < n4; k++) {
130
        CMUL(z[k].re, z[k].im, z[k].re, z[k].im, tcos[k], tsin[k]);
131
    }
132
    for(k = 0; k < n8; k++) {
133
        output[2*k] = -z[n8 + k].im;
134
        output[n2-1-2*k] = z[n8 + k].im;
135

    
136
        output[2*k+1] = z[n8-1-k].re;
137
        output[n2-1-2*k-1] = -z[n8-1-k].re;
138

    
139
        output[n2 + 2*k]=-z[k+n8].re;
140
        output[n-1- 2*k]=-z[k+n8].re;
141

    
142
        output[n2 + 2*k+1]=z[n8-k-1].im;
143
        output[n-2 - 2 * k] = z[n8-k-1].im;
144
    }
145
}
146

    
147
/**
148
 * Compute MDCT of size N = 2^nbits
149
 * @param input N samples
150
 * @param out N/2 samples
151
 * @param tmp temporary storage of N/2 samples
152
 */
153
void ff_mdct_calc(MDCTContext *s, FFTSample *out,
154
                  const FFTSample *input, FFTSample *tmp)
155
{
156
    int i, j, n, n8, n4, n2, n3;
157
    FFTSample re, im, re1, im1;
158
    const uint16_t *revtab = s->fft.revtab;
159
    const FFTSample *tcos = s->tcos;
160
    const FFTSample *tsin = s->tsin;
161
    FFTComplex *x = (FFTComplex *)tmp;
162

    
163
    n = 1 << s->nbits;
164
    n2 = n >> 1;
165
    n4 = n >> 2;
166
    n8 = n >> 3;
167
    n3 = 3 * n4;
168

    
169
    /* pre rotation */
170
    for(i=0;i<n8;i++) {
171
        re = -input[2*i+3*n4] - input[n3-1-2*i];
172
        im = -input[n4+2*i] + input[n4-1-2*i];
173
        j = revtab[i];
174
        CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
175

    
176
        re = input[2*i] - input[n2-1-2*i];
177
        im = -(input[n2+2*i] + input[n-1-2*i]);
178
        j = revtab[n8 + i];
179
        CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
180
    }
181

    
182
    ff_fft_calc(&s->fft, x);
183

    
184
    /* post rotation */
185
    for(i=0;i<n4;i++) {
186
        re = x[i].re;
187
        im = x[i].im;
188
        CMUL(re1, im1, re, im, -tsin[i], -tcos[i]);
189
        out[2*i] = im1;
190
        out[n2-1-2*i] = re1;
191
    }
192
}
193

    
194
void ff_mdct_end(MDCTContext *s)
195
{
196
    av_freep(&s->tcos);
197
    av_freep(&s->tsin);
198
    ff_fft_end(&s->fft);
199
}