]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudiodsp_template.c
Merge remote-tracking branch 'shariman/wmall'
[ffmpeg] / libavcodec / mpegaudiodsp_template.c
1 /*
2  * Copyright (c) 2001, 2002 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdint.h>
22
23 #include "libavutil/mem.h"
24 #include "dct32.h"
25 #include "mathops.h"
26 #include "mpegaudiodsp.h"
27 #include "mpegaudio.h"
28 #include "mpegaudiodata.h"
29
30 #if CONFIG_FLOAT
31 #define RENAME(n) n##_float
32
33 static inline float round_sample(float *sum)
34 {
35     float sum1=*sum;
36     *sum = 0;
37     return sum1;
38 }
39
40 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
41 #define MULS(ra, rb) ((ra)*(rb))
42 #define MULH3(x, y, s) ((s)*(y)*(x))
43 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
44 #define MULLx(x, y, s) ((y)*(x))
45 #define FIXHR(x)        ((float)(x))
46 #define FIXR(x)        ((float)(x))
47 #define SHR(a,b)       ((a)*(1.0f/(1<<(b))))
48
49 #else
50
51 #define RENAME(n) n##_fixed
52 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
53
54 static inline int round_sample(int64_t *sum)
55 {
56     int sum1;
57     sum1 = (int)((*sum) >> OUT_SHIFT);
58     *sum &= (1<<OUT_SHIFT)-1;
59     return av_clip_int16(sum1);
60 }
61
62 #   define MULS(ra, rb) MUL64(ra, rb)
63 #   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
64 #   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
65 #   define MULH3(x, y, s) MULH((s)*(x), y)
66 #   define MULLx(x, y, s) MULL(x,y,s)
67 #   define SHR(a,b)       ((a)>>(b))
68 #   define FIXR(a)        ((int)((a) * FRAC_ONE + 0.5))
69 #   define FIXHR(a)       ((int)((a) * (1LL<<32) + 0.5))
70 #endif
71
72 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
73
74 #define SUM8(op, sum, w, p)               \
75 {                                         \
76     op(sum, (w)[0 * 64], (p)[0 * 64]);    \
77     op(sum, (w)[1 * 64], (p)[1 * 64]);    \
78     op(sum, (w)[2 * 64], (p)[2 * 64]);    \
79     op(sum, (w)[3 * 64], (p)[3 * 64]);    \
80     op(sum, (w)[4 * 64], (p)[4 * 64]);    \
81     op(sum, (w)[5 * 64], (p)[5 * 64]);    \
82     op(sum, (w)[6 * 64], (p)[6 * 64]);    \
83     op(sum, (w)[7 * 64], (p)[7 * 64]);    \
84 }
85
86 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
87 {                                               \
88     INTFLOAT tmp;\
89     tmp = p[0 * 64];\
90     op1(sum1, (w1)[0 * 64], tmp);\
91     op2(sum2, (w2)[0 * 64], tmp);\
92     tmp = p[1 * 64];\
93     op1(sum1, (w1)[1 * 64], tmp);\
94     op2(sum2, (w2)[1 * 64], tmp);\
95     tmp = p[2 * 64];\
96     op1(sum1, (w1)[2 * 64], tmp);\
97     op2(sum2, (w2)[2 * 64], tmp);\
98     tmp = p[3 * 64];\
99     op1(sum1, (w1)[3 * 64], tmp);\
100     op2(sum2, (w2)[3 * 64], tmp);\
101     tmp = p[4 * 64];\
102     op1(sum1, (w1)[4 * 64], tmp);\
103     op2(sum2, (w2)[4 * 64], tmp);\
104     tmp = p[5 * 64];\
105     op1(sum1, (w1)[5 * 64], tmp);\
106     op2(sum2, (w2)[5 * 64], tmp);\
107     tmp = p[6 * 64];\
108     op1(sum1, (w1)[6 * 64], tmp);\
109     op2(sum2, (w2)[6 * 64], tmp);\
110     tmp = p[7 * 64];\
111     op1(sum1, (w1)[7 * 64], tmp);\
112     op2(sum2, (w2)[7 * 64], tmp);\
113 }
114
115 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
116                                   int *dither_state, OUT_INT *samples,
117                                   int incr)
118 {
119     register const MPA_INT *w, *w2, *p;
120     int j;
121     OUT_INT *samples2;
122 #if CONFIG_FLOAT
123     float sum, sum2;
124 #else
125     int64_t sum, sum2;
126 #endif
127
128     /* copy to avoid wrap */
129     memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
130
131     samples2 = samples + 31 * incr;
132     w = window;
133     w2 = window + 31;
134
135     sum = *dither_state;
136     p = synth_buf + 16;
137     SUM8(MACS, sum, w, p);
138     p = synth_buf + 48;
139     SUM8(MLSS, sum, w + 32, p);
140     *samples = round_sample(&sum);
141     samples += incr;
142     w++;
143
144     /* we calculate two samples at the same time to avoid one memory
145        access per two sample */
146     for(j=1;j<16;j++) {
147         sum2 = 0;
148         p = synth_buf + 16 + j;
149         SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
150         p = synth_buf + 48 - j;
151         SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
152
153         *samples = round_sample(&sum);
154         samples += incr;
155         sum += sum2;
156         *samples2 = round_sample(&sum);
157         samples2 -= incr;
158         w++;
159         w2--;
160     }
161
162     p = synth_buf + 32;
163     SUM8(MLSS, sum, w + 32, p);
164     *samples = round_sample(&sum);
165     *dither_state= sum;
166 }
167
168 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
169    32 samples. */
170 void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
171                                  int *synth_buf_offset,
172                                  MPA_INT *window, int *dither_state,
173                                  OUT_INT *samples, int incr,
174                                  MPA_INT *sb_samples)
175 {
176     MPA_INT *synth_buf;
177     int offset;
178
179     offset = *synth_buf_offset;
180     synth_buf = synth_buf_ptr + offset;
181
182     s->RENAME(dct32)(synth_buf, sb_samples);
183     s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
184
185     offset = (offset - 32) & 511;
186     *synth_buf_offset = offset;
187 }
188
189 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
190 {
191     int i, j;
192
193     /* max = 18760, max sum over all 16 coefs : 44736 */
194     for(i=0;i<257;i++) {
195         INTFLOAT v;
196         v = ff_mpa_enwindow[i];
197 #if CONFIG_FLOAT
198         v *= 1.0 / (1LL<<(16 + FRAC_BITS));
199 #endif
200         window[i] = v;
201         if ((i & 63) != 0)
202             v = -v;
203         if (i != 0)
204             window[512 - i] = v;
205     }
206
207     // Needed for avoiding shuffles in ASM implementations
208     for(i=0; i < 8; i++)
209         for(j=0; j < 16; j++)
210             window[512+16*i+j] = window[64*i+32-j];
211
212     for(i=0; i < 8; i++)
213         for(j=0; j < 16; j++)
214             window[512+128+16*i+j] = window[64*i+48-j];
215 }
216
217 /* cos(pi*i/18) */
218 #define C1 FIXHR(0.98480775301220805936/2)
219 #define C2 FIXHR(0.93969262078590838405/2)
220 #define C3 FIXHR(0.86602540378443864676/2)
221 #define C4 FIXHR(0.76604444311897803520/2)
222 #define C5 FIXHR(0.64278760968653932632/2)
223 #define C6 FIXHR(0.5/2)
224 #define C7 FIXHR(0.34202014332566873304/2)
225 #define C8 FIXHR(0.17364817766693034885/2)
226
227 /* 0.5 / cos(pi*(2*i+1)/36) */
228 static const INTFLOAT icos36[9] = {
229     FIXR(0.50190991877167369479),
230     FIXR(0.51763809020504152469),
231     FIXR(0.55168895948124587824),
232     FIXR(0.61038729438072803416),
233     FIXR(0.70710678118654752439),
234     FIXR(0.87172339781054900991),
235     FIXR(1.18310079157624925896),
236     FIXR(1.93185165257813657349),
237     FIXR(5.73685662283492756461),
238 };
239
240 /* 0.5 / cos(pi*(2*i+1)/36) */
241 static const INTFLOAT icos36h[9] = {
242     FIXHR(0.50190991877167369479/2),
243     FIXHR(0.51763809020504152469/2),
244     FIXHR(0.55168895948124587824/2),
245     FIXHR(0.61038729438072803416/2),
246     FIXHR(0.70710678118654752439/2),
247     FIXHR(0.87172339781054900991/2),
248     FIXHR(1.18310079157624925896/4),
249     FIXHR(1.93185165257813657349/4),
250 };
251
252
253 /* using Lee like decomposition followed by hand coded 9 points DCT */
254 void RENAME(ff_imdct36)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
255                         INTFLOAT *win)
256 {
257     int i, j;
258     INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
259     INTFLOAT tmp[18], *tmp1, *in1;
260
261     for(i=17;i>=1;i--)
262         in[i] += in[i-1];
263     for(i=17;i>=3;i-=2)
264         in[i] += in[i-2];
265
266     for(j=0;j<2;j++) {
267         tmp1 = tmp + j;
268         in1 = in + j;
269
270         t2 = in1[2*4] + in1[2*8] - in1[2*2];
271
272         t3 = in1[2*0] + SHR(in1[2*6],1);
273         t1 = in1[2*0] - in1[2*6];
274         tmp1[ 6] = t1 - SHR(t2,1);
275         tmp1[16] = t1 + t2;
276
277         t0 = MULH3(in1[2*2] + in1[2*4] ,    C2, 2);
278         t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
279         t2 = MULH3(in1[2*2] + in1[2*8] ,   -C4, 2);
280
281         tmp1[10] = t3 - t0 - t2;
282         tmp1[ 2] = t3 + t0 + t1;
283         tmp1[14] = t3 + t2 - t1;
284
285         tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
286         t2 = MULH3(in1[2*1] + in1[2*5],    C1, 2);
287         t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
288         t0 = MULH3(in1[2*3], C3, 2);
289
290         t1 = MULH3(in1[2*1] + in1[2*7],   -C5, 2);
291
292         tmp1[ 0] = t2 + t3 + t0;
293         tmp1[12] = t2 + t1 - t0;
294         tmp1[ 8] = t3 - t1 - t0;
295     }
296
297     i = 0;
298     for(j=0;j<4;j++) {
299         t0 = tmp[i];
300         t1 = tmp[i + 2];
301         s0 = t1 + t0;
302         s2 = t1 - t0;
303
304         t2 = tmp[i + 1];
305         t3 = tmp[i + 3];
306         s1 = MULH3(t3 + t2, icos36h[j], 2);
307         s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
308
309         t0 = s0 + s1;
310         t1 = s0 - s1;
311         out[(9 + j)*SBLIMIT] =  MULH3(t1, win[9 + j], 1) + buf[9 + j];
312         out[(8 - j)*SBLIMIT] =  MULH3(t1, win[8 - j], 1) + buf[8 - j];
313         buf[9 + j] = MULH3(t0, win[20 + 9 + j], 1);
314         buf[8 - j] = MULH3(t0, win[20 + 8 - j], 1);
315
316         t0 = s2 + s3;
317         t1 = s2 - s3;
318         out[(9 + 8 - j)*SBLIMIT] =  MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
319         out[(        j)*SBLIMIT] =  MULH3(t1, win[        j], 1) + buf[        j];
320         buf[9 + 8 - j] = MULH3(t0, win[20 + 9 + 8 - j], 1);
321         buf[      + j] = MULH3(t0, win[20         + j], 1);
322         i += 4;
323     }
324
325     s0 = tmp[16];
326     s1 = MULH3(tmp[17], icos36h[4], 2);
327     t0 = s0 + s1;
328     t1 = s0 - s1;
329     out[(9 + 4)*SBLIMIT] =  MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
330     out[(8 - 4)*SBLIMIT] =  MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
331     buf[9 + 4] = MULH3(t0, win[20 + 9 + 4], 1);
332     buf[8 - 4] = MULH3(t0, win[20 + 8 - 4], 1);
333 }
334