Mercurial > libavcodec.hg
annotate mdct.c @ 6139:5077d1562573 libavcodec
Make the Kaiser-Bessel window generator a common function
Patch by Robert Swain, robert d swain a gmail d com
author | andoma |
---|---|
date | Sat, 12 Jan 2008 11:11:19 +0000 |
parents | c8c591fe26f8 |
children | a35b838ab955 |
rev | line source |
---|---|
781 | 1 /* |
2 * MDCT/IMDCT transforms | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
781 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
781 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
781 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
781 | 20 */ |
21 #include "dsputil.h" | |
22 | |
1106 | 23 /** |
24 * @file mdct.c | |
25 * MDCT/IMDCT transforms. | |
26 */ | |
27 | |
6139
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
28 // Generate a Kaiser-Bessel Derived Window. |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
29 void ff_kbd_window_init(float *window) |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
30 { |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
31 int i, j; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
32 double sum = 0.0, bessel, tmp; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
33 double local_window[256]; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
34 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0); |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
35 |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
36 for (i = 0; i < 256; i++) { |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
37 tmp = i * (256 - i) * alpha2; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
38 bessel = 1.0; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
39 for (j = 100; j > 0; j--) /* default to 100 iterations */ |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
40 bessel = bessel * tmp / (j * j) + 1; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
41 sum += bessel; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
42 local_window[i] = sum; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
43 } |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
44 |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
45 sum++; |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
46 for (i = 0; i < 256; i++) |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
47 window[i] = sqrt(local_window[i] / sum); |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
48 } |
5077d1562573
Make the Kaiser-Bessel window generator a common function
andoma
parents:
3947
diff
changeset
|
49 |
1106 | 50 /** |
51 * init MDCT or IMDCT computation. | |
781 | 52 */ |
794 | 53 int ff_mdct_init(MDCTContext *s, int nbits, int inverse) |
781 | 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; | |
970 | 63 s->tcos = av_malloc(n4 * sizeof(FFTSample)); |
781 | 64 if (!s->tcos) |
65 goto fail; | |
970 | 66 s->tsin = av_malloc(n4 * sizeof(FFTSample)); |
781 | 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 } | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
75 if (ff_fft_init(&s->fft, s->nbits - 2, inverse) < 0) |
781 | 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 */ | |
2967 | 101 void ff_imdct_calc(MDCTContext *s, FFTSample *output, |
794 | 102 const FFTSample *input, FFTSample *tmp) |
781 | 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 } | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
125 ff_fft_calc(&s->fft, z); |
781 | 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 */ | |
2967 | 153 void ff_mdct_calc(MDCTContext *s, FFTSample *out, |
794 | 154 const FFTSample *input, FFTSample *tmp) |
781 | 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 | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
182 ff_fft_calc(&s->fft, x); |
2967 | 183 |
781 | 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 | |
794 | 194 void ff_mdct_end(MDCTContext *s) |
781 | 195 { |
196 av_freep(&s->tcos); | |
197 av_freep(&s->tsin); | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
198 ff_fft_end(&s->fft); |
781 | 199 } |