]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_imdct.c
95b7aa2969c418bc93efd93ebff81e214eadd64b
[vlc] / src / ac3_decoder / ac3_imdct.c
1 #include <math.h>
2
3 #include "int_types.h"
4 #include "ac3_decoder.h"
5 #include "ac3_internal.h"
6
7 void imdct_do_256(float x[],float y[],float delay[]);
8 void imdct_do_512(float x[],float y[],float delay[]);
9
10 typedef struct complex_s {
11     float real;
12     float imag;
13 } complex_t;
14
15 #define N 512
16
17 static complex_t buf[N/4];
18
19 /* 128 point bit-reverse LUT */
20 static u8 bit_reverse_512[] = {
21     0x00, 0x40, 0x20, 0x60, 0x10, 0x50, 0x30, 0x70,
22     0x08, 0x48, 0x28, 0x68, 0x18, 0x58, 0x38, 0x78,
23     0x04, 0x44, 0x24, 0x64, 0x14, 0x54, 0x34, 0x74,
24     0x0c, 0x4c, 0x2c, 0x6c, 0x1c, 0x5c, 0x3c, 0x7c,
25     0x02, 0x42, 0x22, 0x62, 0x12, 0x52, 0x32, 0x72,
26     0x0a, 0x4a, 0x2a, 0x6a, 0x1a, 0x5a, 0x3a, 0x7a,
27     0x06, 0x46, 0x26, 0x66, 0x16, 0x56, 0x36, 0x76,
28     0x0e, 0x4e, 0x2e, 0x6e, 0x1e, 0x5e, 0x3e, 0x7e,
29     0x01, 0x41, 0x21, 0x61, 0x11, 0x51, 0x31, 0x71,
30     0x09, 0x49, 0x29, 0x69, 0x19, 0x59, 0x39, 0x79,
31     0x05, 0x45, 0x25, 0x65, 0x15, 0x55, 0x35, 0x75,
32     0x0d, 0x4d, 0x2d, 0x6d, 0x1d, 0x5d, 0x3d, 0x7d,
33     0x03, 0x43, 0x23, 0x63, 0x13, 0x53, 0x33, 0x73,
34     0x0b, 0x4b, 0x2b, 0x6b, 0x1b, 0x5b, 0x3b, 0x7b,
35     0x07, 0x47, 0x27, 0x67, 0x17, 0x57, 0x37, 0x77,
36     0x0f, 0x4f, 0x2f, 0x6f, 0x1f, 0x5f, 0x3f, 0x7f};
37
38 static u8 bit_reverse_256[] = {
39     0x00, 0x20, 0x10, 0x30, 0x08, 0x28, 0x18, 0x38,
40     0x04, 0x24, 0x14, 0x34, 0x0c, 0x2c, 0x1c, 0x3c,
41     0x02, 0x22, 0x12, 0x32, 0x0a, 0x2a, 0x1a, 0x3a,
42     0x06, 0x26, 0x16, 0x36, 0x0e, 0x2e, 0x1e, 0x3e,
43     0x01, 0x21, 0x11, 0x31, 0x09, 0x29, 0x19, 0x39,
44     0x05, 0x25, 0x15, 0x35, 0x0d, 0x2d, 0x1d, 0x3d,
45     0x03, 0x23, 0x13, 0x33, 0x0b, 0x2b, 0x1b, 0x3b,
46     0x07, 0x27, 0x17, 0x37, 0x0f, 0x2f, 0x1f, 0x3f};
47
48 /* Twiddle factor LUT */
49 static complex_t *w[7];
50 static complex_t w_1[1];
51 static complex_t w_2[2];
52 static complex_t w_4[4];
53 static complex_t w_8[8];
54 static complex_t w_16[16];
55 static complex_t w_32[32];
56 static complex_t w_64[64];
57
58 /* Twiddle factors for IMDCT */
59 static float xcos1[N/4];
60 static float xsin1[N/4];
61 static float xcos2[N/8];
62 static float xsin2[N/8];
63
64 /* Delay buffer for time domain interleaving */
65 static float delay[6][256];
66
67 /* Windowing function for Modified DCT - Thank you acroread */
68 static float window[] = {
69     0.00014, 0.00024, 0.00037, 0.00051, 0.00067, 0.00086, 0.00107, 0.00130,
70     0.00157, 0.00187, 0.00220, 0.00256, 0.00297, 0.00341, 0.00390, 0.00443,
71     0.00501, 0.00564, 0.00632, 0.00706, 0.00785, 0.00871, 0.00962, 0.01061,
72     0.01166, 0.01279, 0.01399, 0.01526, 0.01662, 0.01806, 0.01959, 0.02121,
73     0.02292, 0.02472, 0.02662, 0.02863, 0.03073, 0.03294, 0.03527, 0.03770,
74     0.04025, 0.04292, 0.04571, 0.04862, 0.05165, 0.05481, 0.05810, 0.06153,
75     0.06508, 0.06878, 0.07261, 0.07658, 0.08069, 0.08495, 0.08935, 0.09389,
76     0.09859, 0.10343, 0.10842, 0.11356, 0.11885, 0.12429, 0.12988, 0.13563,
77     0.14152, 0.14757, 0.15376, 0.16011, 0.16661, 0.17325, 0.18005, 0.18699,
78     0.19407, 0.20130, 0.20867, 0.21618, 0.22382, 0.23161, 0.23952, 0.24757,
79     0.25574, 0.26404, 0.27246, 0.28100, 0.28965, 0.29841, 0.30729, 0.31626,
80     0.32533, 0.33450, 0.34376, 0.35311, 0.36253, 0.37204, 0.38161, 0.39126,
81     0.40096, 0.41072, 0.42054, 0.43040, 0.44030, 0.45023, 0.46020, 0.47019,
82     0.48020, 0.49022, 0.50025, 0.51028, 0.52031, 0.53033, 0.54033, 0.55031,
83     0.56026, 0.57019, 0.58007, 0.58991, 0.59970, 0.60944, 0.61912, 0.62873,
84     0.63827, 0.64774, 0.65713, 0.66643, 0.67564, 0.68476, 0.69377, 0.70269,
85     0.71150, 0.72019, 0.72877, 0.73723, 0.74557, 0.75378, 0.76186, 0.76981,
86     0.77762, 0.78530, 0.79283, 0.80022, 0.80747, 0.81457, 0.82151, 0.82831,
87     0.83496, 0.84145, 0.84779, 0.85398, 0.86001, 0.86588, 0.87160, 0.87716,
88     0.88257, 0.88782, 0.89291, 0.89785, 0.90264, 0.90728, 0.91176, 0.91610,
89     0.92028, 0.92432, 0.92822, 0.93197, 0.93558, 0.93906, 0.94240, 0.94560,
90     0.94867, 0.95162, 0.95444, 0.95713, 0.95971, 0.96217, 0.96451, 0.96674,
91     0.96887, 0.97089, 0.97281, 0.97463, 0.97635, 0.97799, 0.97953, 0.98099,
92     0.98236, 0.98366, 0.98488, 0.98602, 0.98710, 0.98811, 0.98905, 0.98994,
93     0.99076, 0.99153, 0.99225, 0.99291, 0.99353, 0.99411, 0.99464, 0.99513,
94     0.99558, 0.99600, 0.99639, 0.99674, 0.99706, 0.99736, 0.99763, 0.99788,
95     0.99811, 0.99831, 0.99850, 0.99867, 0.99882, 0.99895, 0.99908, 0.99919,
96     0.99929, 0.99938, 0.99946, 0.99953, 0.99959, 0.99965, 0.99969, 0.99974,
97     0.99978, 0.99981, 0.99984, 0.99986, 0.99988, 0.99990, 0.99992, 0.99993,
98     0.99994, 0.99995, 0.99996, 0.99997, 0.99998, 0.99998, 0.99998, 0.99999,
99     0.99999, 0.99999, 0.99999, 1.00000, 1.00000, 1.00000, 1.00000, 1.00000,
100     1.00000, 1.00000, 1.00000, 1.00000, 1.00000, 1.00000, 1.00000, 1.00000 };
101
102 static __inline__ void swap_cmplx(complex_t *a, complex_t *b)
103 {
104     complex_t tmp;
105
106     tmp = *a;
107     *a = *b;
108     *b = tmp;
109 }
110
111 static __inline__ complex_t cmplx_mult(complex_t a, complex_t b)
112 {
113     complex_t ret;
114
115     ret.real = a.real * b.real - a.imag * b.imag;
116     ret.imag = a.real * b.imag + a.imag * b.real;
117
118     return ret;
119 }
120
121 static void imdct_init(void) __attribute__ ((__constructor__));
122 static void imdct_init(void)
123 {
124     int i,k;
125     complex_t angle_step;
126     complex_t current_angle;
127
128     /* Twiddle factors to turn IFFT into IMDCT */
129     for (i=0; i < N/4; i++) {
130         xcos1[i] = -cos(2 * M_PI * (8*i+1)/(8*N)) ;
131         xsin1[i] = -sin(2 * M_PI * (8*i+1)/(8*N)) ;
132     }
133
134     /* More twiddle factors to turn IFFT into IMDCT */
135     for (i=0; i < N/8; i++) {
136         xcos2[i] = -cos(2 * M_PI * (8*i+1)/(4*N)) ;
137         xsin2[i] = -sin(2 * M_PI * (8*i+1)/(4*N)) ;
138     }
139
140     /* Canonical twiddle factors for FFT */
141     w[0] = w_1;
142     w[1] = w_2;
143     w[2] = w_4;
144     w[3] = w_8;
145     w[4] = w_16;
146     w[5] = w_32;
147     w[6] = w_64;
148
149     for (i = 0; i < 7; i++) {
150         angle_step.real = cos(-2.0f * M_PI / (1 << (i+1)));
151         angle_step.imag = sin(-2.0f * M_PI / (1 << (i+1)));
152
153         current_angle.real = 1.0f;
154         current_angle.imag = 0.0f;
155
156         for (k = 0; k < 1 << i; k++) {
157             w[i][k] = current_angle;
158             current_angle = cmplx_mult(current_angle,angle_step);
159         }
160     }
161 }
162
163 void imdct (ac3dec_t * p_ac3dec)
164 {
165     int i;
166
167     for (i=0; i<p_ac3dec->bsi.nfchans;i++) {
168         if (p_ac3dec->audblk.blksw[i])
169             imdct_do_256(p_ac3dec->coeffs.fbw[i],p_ac3dec->samples.channel[i],delay[i]);
170         else
171             imdct_do_512(p_ac3dec->coeffs.fbw[i],p_ac3dec->samples.channel[i],delay[i]);
172     }
173
174     /* XXX?? We don't bother with the IMDCT for the LFE as it's currently
175      * unused. */
176     //if (bsi->lfeon)
177     //    imdct_do_512(coeffs->lfe,samples->channel[5],delay[5]);
178 }
179
180 void
181 imdct_do_512(float x[],float y[],float delay[])
182 {
183     int i,k;
184     int p,q;
185     int m;
186     int two_m;
187     int two_m_plus_one;
188
189     float tmp_a_i;
190     float tmp_a_r;
191     float tmp_b_i;
192     float tmp_b_r;
193
194
195     float *y_ptr;
196     float *delay_ptr;
197     float *window_ptr;
198
199     /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
200     for (i=0; i < N/4; i++) {
201         /* z[i] = (X[N/2-2*i-1] + j * X[2*i]) * (xcos1[i] + j * xsin1[i]) ; */
202         buf[i].real =  (x[N/2-2*i-1] * xcos1[i])  - (x[2*i]       * xsin1[i]);
203         buf[i].imag = -((x[2*i]       * xcos1[i])  + (x[N/2-2*i-1] * xsin1[i]));
204     }
205
206     /* Bit reversed shuffling */
207     for (i=0; i<N/4; i++) {
208         k = bit_reverse_512[i];
209         if (k < i)
210             swap_cmplx(&buf[i],&buf[k]);
211     }
212
213     /* FFT Merge */
214     for (m=0; m < 7; m++) {
215         two_m = (1 << m);
216         two_m_plus_one = (1 << (m+1));
217
218         for (k = 0; k < two_m; k++) {
219             for (i = 0; i < 128; i += two_m_plus_one) {
220                 p = k + i;
221                 q = p + two_m;
222                 tmp_a_r = buf[p].real;
223                 tmp_a_i = buf[p].imag;
224                 tmp_b_r = buf[q].real * w[m][k].real - buf[q].imag * w[m][k].imag;
225                 tmp_b_i = buf[q].imag * w[m][k].real + buf[q].real * w[m][k].imag;
226                 buf[p].real = tmp_a_r + tmp_b_r;
227                 buf[p].imag =  tmp_a_i + tmp_b_i;
228                 buf[q].real = tmp_a_r - tmp_b_r;
229                 buf[q].imag =  tmp_a_i - tmp_b_i;
230             }
231         }
232     }
233
234     /* Post IFFT complex multiply  plus IFFT complex conjugate*/
235     for (i=0; i < N/4; i++) {
236         /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
237         tmp_a_r =   buf[i].real;
238         tmp_a_i = - buf[i].imag;
239         buf[i].real =(tmp_a_r * xcos1[i])  - (tmp_a_i  * xsin1[i]);
240         buf[i].imag =(tmp_a_r * xsin1[i])  + (tmp_a_i  * xcos1[i]);
241     }
242
243     y_ptr = y;
244     delay_ptr = delay;
245     window_ptr = window;
246     /* Window and convert to real valued signal */
247     for (i=0; i<N/8; i++) {
248         *y_ptr++   = 2.0f * (-buf[N/8+i].imag   * *window_ptr++ + *delay_ptr++);
249         *y_ptr++   = 2.0f * (buf[N/8-i-1].real * *window_ptr++ + *delay_ptr++);
250     }
251
252     for (i=0; i<N/8; i++) {
253         *y_ptr++  = 2.0f * (-buf[i].real       * *window_ptr++ + *delay_ptr++);
254         *y_ptr++  = 2.0f * (buf[N/4-i-1].imag * *window_ptr++ + *delay_ptr++);
255     }
256
257     /* The trailing edge of the window goes into the delay line */
258     delay_ptr = delay;
259
260     for (i=0; i<N/8; i++) {
261         *delay_ptr++  = -buf[N/8+i].real   * *--window_ptr;
262         *delay_ptr++  =  buf[N/8-i-1].imag * *--window_ptr;
263     }
264
265     for (i=0; i<N/8; i++) {
266         *delay_ptr++  =  buf[i].imag       * *--window_ptr;
267         *delay_ptr++  = -buf[N/4-i-1].real * *--window_ptr;
268     }
269 }
270
271 void
272 imdct_do_256(float x[],float y[],float delay[])
273 {
274     int i,k;
275     int p,q;
276     int m;
277     int two_m;
278     int two_m_plus_one;
279
280     float tmp_a_i;
281     float tmp_a_r;
282     float tmp_b_i;
283     float tmp_b_r;
284
285     complex_t *buf_1, *buf_2;
286
287     buf_1 = &buf[0];
288     buf_2 = &buf[64];
289
290     /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
291     for (k=0; k<N/8; k++) {
292         /* X1[k] = X[2*k]  */
293         /* X2[k] = X[2*k+1]     */
294
295         p = 2 * (N/4-2*k-1);
296         q = 2 * (2 * k);
297
298        /* Z1[k] = (X1[N/4-2*k-1] + j * X1[2*k]) * (xcos2[k] + j * xsin2[k]); */
299         buf_1[k].real =    x[p] * xcos2[k] - x[q] * xsin2[k];
300         buf_1[k].imag = - (x[q] * xcos2[k] + x[p] * xsin2[k]);
301        /* Z2[k] = (X2[N/4-2*k-1] + j * X2[2*k]) * (xcos2[k] + j * xsin2[k]); */
302         buf_2[k].real =    x[p + 1] * xcos2[k] - x[q + 1] * xsin2[k];
303         buf_2[k].imag = - (x[q + 1] * xcos2[k] + x[p + 1] * xsin2[k]);
304     }
305
306     /* IFFT Bit reversed shuffling */
307     for (i=0; i<N/8; i++) {
308         k = bit_reverse_256[i];
309         if (k < i) {
310             swap_cmplx(&buf_1[i],&buf_1[k]);
311             swap_cmplx(&buf_2[i],&buf_2[k]);
312         }
313     }
314
315     /* FFT Merge */
316     for (m=0; m < 6; m++) {
317         two_m = (1 << m);
318         two_m_plus_one = (1 << (m+1));
319
320         for (k = 0; k < two_m; k++) {
321             for (i = 0; i < 64; i += two_m_plus_one) {
322                 p = k + i;
323                 q = p + two_m;
324                 /* Do block 1 */
325                 tmp_a_r = buf_1[p].real;
326                 tmp_a_i = buf_1[p].imag;
327                 tmp_b_r = buf_1[q].real * w[m][k].real - buf_1[q].imag * w[m][k].imag;
328                 tmp_b_i = buf_1[q].imag * w[m][k].real + buf_1[q].real * w[m][k].imag;
329                 buf_1[p].real = tmp_a_r + tmp_b_r;
330                 buf_1[p].imag =  tmp_a_i + tmp_b_i;
331                 buf_1[q].real = tmp_a_r - tmp_b_r;
332                 buf_1[q].imag =  tmp_a_i - tmp_b_i;
333
334                 /* Do block 2 */
335                 tmp_a_r = buf_2[p].real;
336                 tmp_a_i = buf_2[p].imag;
337                 tmp_b_r = buf_2[q].real * w[m][k].real - buf_2[q].imag * w[m][k].imag;
338                 tmp_b_i = buf_2[q].imag * w[m][k].real + buf_2[q].real * w[m][k].imag;
339                 buf_2[p].real = tmp_a_r + tmp_b_r;
340                 buf_2[p].imag =  tmp_a_i + tmp_b_i;
341                 buf_2[q].real = tmp_a_r - tmp_b_r;
342                 buf_2[q].imag =  tmp_a_i - tmp_b_i;
343             }
344         }
345     }
346
347     /* Post IFFT complex multiply */
348     for (i=0; i < N/8; i++) {
349         /* y1[n] = z1[n] * (xcos2[n] + j * xs in2[n]) ; */
350         tmp_a_r =   buf_1[i].real;
351         tmp_a_i = - buf_1[i].imag;
352         buf_1[i].real =(tmp_a_r * xcos2[i])  - (tmp_a_i  * xsin2[i]);
353         buf_1[i].imag =(tmp_a_r * xsin2[i])  + (tmp_a_i  * xcos2[i]);
354         /* y2[n] = z2[n] * (xcos2[n] + j * xsin2[n]) ; */
355         tmp_a_r =   buf_2[i].real;
356         tmp_a_i = - buf_2[i].imag;
357         buf_2[i].real =(tmp_a_r * xcos2[i])  - (tmp_a_i  * xsin2[i]);
358         buf_2[i].imag =(tmp_a_r * xsin2[i])  + (tmp_a_i  * xcos2[i]);
359     }
360
361     /* Window and convert to real valued signal */
362     for (i=0; i<N/8; i++) {
363         y[2*i]         = -buf_1[i].imag       * window[2*i];
364         y[2*i+1]       =  buf_1[N/8-i-1].real * window[2*i+1];
365         y[N/4+2*i]     = -buf_1[i].real       * window[N/4+2*i];
366         y[N/4+2*i+1]   =  buf_1[N/8-i-1].imag * window[N/4+2*i+1];
367         y[N/2+2*i]     = -buf_2[i].real       * window[N/2-2*i-1];
368         y[N/2+2*i+1]   =  buf_2[N/8-i-1].imag * window[N/2-2*i-2];
369         y[3*N/4+2*i]   =  buf_2[i].imag       * window[N/4-2*i-1];
370         y[3*N/4+2*i+1] = -buf_2[N/8-i-1].real * window[N/4-2*i-2];
371     }
372
373     /* Overlap and add */
374     for (i=0; i<N/2; i++) {
375         y[i] = 2 * (y[i] + delay[i]);
376         delay[i] = y[N/2+i];
377     }
378 }