]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudiodsp_template.c
c67c456e8a75d3331f994cd7f5fc1ef14cdc6b8a
[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/attributes.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/thread.h"
26 #include "dct32.h"
27 #include "mathops.h"
28 #include "mpegaudiodsp.h"
29 #include "mpegaudio.h"
30
31 #if USE_FLOATS
32 #define RENAME(n) n##_float
33
34 static inline float round_sample(float *sum)
35 {
36     float sum1=*sum;
37     *sum = 0;
38     return sum1;
39 }
40
41 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
42 #define MULS(ra, rb) ((ra)*(rb))
43 #define MULH3(x, y, s) ((s)*(y)*(x))
44 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
45 #define MULLx(x, y, s) ((y)*(x))
46 #define FIXHR(x)        ((float)(x))
47 #define FIXR(x)        ((float)(x))
48 #define SHR(a,b)       ((a)*(1.0f/(1<<(b))))
49
50 #else
51
52 #define RENAME(n) n##_fixed
53 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
54
55 static inline int round_sample(int64_t *sum)
56 {
57     int sum1;
58     sum1 = (int)((*sum) >> OUT_SHIFT);
59     *sum &= (1<<OUT_SHIFT)-1;
60     return av_clip_int16(sum1);
61 }
62
63 #   define MULS(ra, rb) MUL64(ra, rb)
64 #   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
65 #   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
66 #   define MULH3(x, y, s) MULH((s)*(x), y)
67 #   define MULLx(x, y, s) MULL((int)(x),(y),s)
68 #   define SHR(a,b)       (((int)(a))>>(b))
69 #   define FIXR(a)        ((int)((a) * FRAC_ONE + 0.5))
70 #   define FIXHR(a)       ((int)((a) * (1LL<<32) + 0.5))
71 #endif
72
73 /** Window for MDCT. Actually only the elements in [0,17] and
74     [MDCT_BUF_SIZE/2, MDCT_BUF_SIZE/2 + 17] are actually used. The rest
75     is just to preserve alignment for SIMD implementations.
76 */
77 DECLARE_ALIGNED(16, INTFLOAT, RENAME(ff_mdct_win))[8][MDCT_BUF_SIZE];
78
79 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
80
81 #define SUM8(op, sum, w, p)               \
82 {                                         \
83     op(sum, (w)[0 * 64], (p)[0 * 64]);    \
84     op(sum, (w)[1 * 64], (p)[1 * 64]);    \
85     op(sum, (w)[2 * 64], (p)[2 * 64]);    \
86     op(sum, (w)[3 * 64], (p)[3 * 64]);    \
87     op(sum, (w)[4 * 64], (p)[4 * 64]);    \
88     op(sum, (w)[5 * 64], (p)[5 * 64]);    \
89     op(sum, (w)[6 * 64], (p)[6 * 64]);    \
90     op(sum, (w)[7 * 64], (p)[7 * 64]);    \
91 }
92
93 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
94 {                                               \
95     INTFLOAT tmp;\
96     tmp = p[0 * 64];\
97     op1(sum1, (w1)[0 * 64], tmp);\
98     op2(sum2, (w2)[0 * 64], tmp);\
99     tmp = p[1 * 64];\
100     op1(sum1, (w1)[1 * 64], tmp);\
101     op2(sum2, (w2)[1 * 64], tmp);\
102     tmp = p[2 * 64];\
103     op1(sum1, (w1)[2 * 64], tmp);\
104     op2(sum2, (w2)[2 * 64], tmp);\
105     tmp = p[3 * 64];\
106     op1(sum1, (w1)[3 * 64], tmp);\
107     op2(sum2, (w2)[3 * 64], tmp);\
108     tmp = p[4 * 64];\
109     op1(sum1, (w1)[4 * 64], tmp);\
110     op2(sum2, (w2)[4 * 64], tmp);\
111     tmp = p[5 * 64];\
112     op1(sum1, (w1)[5 * 64], tmp);\
113     op2(sum2, (w2)[5 * 64], tmp);\
114     tmp = p[6 * 64];\
115     op1(sum1, (w1)[6 * 64], tmp);\
116     op2(sum2, (w2)[6 * 64], tmp);\
117     tmp = p[7 * 64];\
118     op1(sum1, (w1)[7 * 64], tmp);\
119     op2(sum2, (w2)[7 * 64], tmp);\
120 }
121
122 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
123                                   int *dither_state, OUT_INT *samples,
124                                   ptrdiff_t incr)
125 {
126     register const MPA_INT *w, *w2, *p;
127     int j;
128     OUT_INT *samples2;
129 #if USE_FLOATS
130     float sum, sum2;
131 #else
132     int64_t sum, sum2;
133 #endif
134
135     /* copy to avoid wrap */
136     memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
137
138     samples2 = samples + 31 * incr;
139     w = window;
140     w2 = window + 31;
141
142     sum = *dither_state;
143     p = synth_buf + 16;
144     SUM8(MACS, sum, w, p);
145     p = synth_buf + 48;
146     SUM8(MLSS, sum, w + 32, p);
147     *samples = round_sample(&sum);
148     samples += incr;
149     w++;
150
151     /* we calculate two samples at the same time to avoid one memory
152        access per two sample */
153     for(j=1;j<16;j++) {
154         sum2 = 0;
155         p = synth_buf + 16 + j;
156         SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
157         p = synth_buf + 48 - j;
158         SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
159
160         *samples = round_sample(&sum);
161         samples += incr;
162         sum += sum2;
163         *samples2 = round_sample(&sum);
164         samples2 -= incr;
165         w++;
166         w2--;
167     }
168
169     p = synth_buf + 32;
170     SUM8(MLSS, sum, w + 32, p);
171     *samples = round_sample(&sum);
172     *dither_state= sum;
173 }
174
175 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
176    32 samples. */
177 void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
178                                  int *synth_buf_offset,
179                                  MPA_INT *window, int *dither_state,
180                                  OUT_INT *samples, ptrdiff_t incr,
181                                  MPA_INT *sb_samples)
182 {
183     MPA_INT *synth_buf;
184     int offset;
185
186     offset = *synth_buf_offset;
187     synth_buf = synth_buf_ptr + offset;
188
189     s->RENAME(dct32)(synth_buf, sb_samples);
190     s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
191
192     offset = (offset - 32) & 511;
193     *synth_buf_offset = offset;
194 }
195
196 static av_cold void mpa_synth_init(MPA_INT *window)
197 {
198     int i, j;
199
200     /* max = 18760, max sum over all 16 coefs : 44736 */
201     for(i=0;i<257;i++) {
202         INTFLOAT v;
203         v = ff_mpa_enwindow[i];
204 #if USE_FLOATS
205         v *= 1.0 / (1LL<<(16 + FRAC_BITS));
206 #endif
207         window[i] = v;
208         if ((i & 63) != 0)
209             v = -v;
210         if (i != 0)
211             window[512 - i] = v;
212     }
213
214
215     // Needed for avoiding shuffles in ASM implementations
216     for(i=0; i < 8; i++)
217         for(j=0; j < 16; j++)
218             window[512+16*i+j] = window[64*i+32-j];
219
220     for(i=0; i < 8; i++)
221         for(j=0; j < 16; j++)
222             window[512+128+16*i+j] = window[64*i+48-j];
223 }
224
225 static av_cold void mpa_synth_window_init(void)
226 {
227     mpa_synth_init(RENAME(ff_mpa_synth_window));
228 }
229
230 av_cold void RENAME(ff_mpa_synth_init)(void)
231 {
232     static AVOnce init_static_once = AV_ONCE_INIT;
233     ff_thread_once(&init_static_once, mpa_synth_window_init);
234 }
235
236 /* cos(pi*i/18) */
237 #define C1 FIXHR(0.98480775301220805936/2)
238 #define C2 FIXHR(0.93969262078590838405/2)
239 #define C3 FIXHR(0.86602540378443864676/2)
240 #define C4 FIXHR(0.76604444311897803520/2)
241 #define C5 FIXHR(0.64278760968653932632/2)
242 #define C6 FIXHR(0.5/2)
243 #define C7 FIXHR(0.34202014332566873304/2)
244 #define C8 FIXHR(0.17364817766693034885/2)
245
246 /* 0.5 / cos(pi*(2*i+1)/36) */
247 static const INTFLOAT icos36[9] = {
248     FIXR(0.50190991877167369479),
249     FIXR(0.51763809020504152469), //0
250     FIXR(0.55168895948124587824),
251     FIXR(0.61038729438072803416),
252     FIXR(0.70710678118654752439), //1
253     FIXR(0.87172339781054900991),
254     FIXR(1.18310079157624925896),
255     FIXR(1.93185165257813657349), //2
256     FIXR(5.73685662283492756461),
257 };
258
259 /* 0.5 / cos(pi*(2*i+1)/36) */
260 static const INTFLOAT icos36h[9] = {
261     FIXHR(0.50190991877167369479/2),
262     FIXHR(0.51763809020504152469/2), //0
263     FIXHR(0.55168895948124587824/2),
264     FIXHR(0.61038729438072803416/2),
265     FIXHR(0.70710678118654752439/2), //1
266     FIXHR(0.87172339781054900991/2),
267     FIXHR(1.18310079157624925896/4),
268     FIXHR(1.93185165257813657349/4), //2
269 //    FIXHR(5.73685662283492756461),
270 };
271
272 /* using Lee like decomposition followed by hand coded 9 points DCT */
273 static void imdct36(INTFLOAT *out, INTFLOAT *buf, SUINTFLOAT *in, INTFLOAT *win)
274 {
275     int i, j;
276     SUINTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
277     SUINTFLOAT tmp[18], *tmp1, *in1;
278
279     for (i = 17; i >= 1; i--)
280         in[i] += in[i-1];
281     for (i = 17; i >= 3; i -= 2)
282         in[i] += in[i-2];
283
284     for (j = 0; j < 2; j++) {
285         tmp1 = tmp + j;
286         in1 = in + j;
287
288         t2 = in1[2*4] + in1[2*8] - in1[2*2];
289
290         t3 = in1[2*0] + SHR(in1[2*6],1);
291         t1 = in1[2*0] - in1[2*6];
292         tmp1[ 6] = t1 - SHR(t2,1);
293         tmp1[16] = t1 + t2;
294
295         t0 = MULH3(in1[2*2] + in1[2*4] ,    C2, 2);
296         t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
297         t2 = MULH3(in1[2*2] + in1[2*8] ,   -C4, 2);
298
299         tmp1[10] = t3 - t0 - t2;
300         tmp1[ 2] = t3 + t0 + t1;
301         tmp1[14] = t3 + t2 - t1;
302
303         tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
304         t2 = MULH3(in1[2*1] + in1[2*5],    C1, 2);
305         t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
306         t0 = MULH3(in1[2*3], C3, 2);
307
308         t1 = MULH3(in1[2*1] + in1[2*7],   -C5, 2);
309
310         tmp1[ 0] = t2 + t3 + t0;
311         tmp1[12] = t2 + t1 - t0;
312         tmp1[ 8] = t3 - t1 - t0;
313     }
314
315     i = 0;
316     for (j = 0; j < 4; j++) {
317         t0 = tmp[i];
318         t1 = tmp[i + 2];
319         s0 = t1 + t0;
320         s2 = t1 - t0;
321
322         t2 = tmp[i + 1];
323         t3 = tmp[i + 3];
324         s1 = MULH3(t3 + t2, icos36h[    j], 2);
325         s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
326
327         t0 = s0 + s1;
328         t1 = s0 - s1;
329         out[(9 + j) * SBLIMIT] = MULH3(t1, win[     9 + j], 1) + buf[4*(9 + j)];
330         out[(8 - j) * SBLIMIT] = MULH3(t1, win[     8 - j], 1) + buf[4*(8 - j)];
331         buf[4 * ( 9 + j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
332         buf[4 * ( 8 - j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
333
334         t0 = s2 + s3;
335         t1 = s2 - s3;
336         out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[     9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
337         out[         j  * SBLIMIT] = MULH3(t1, win[             j], 1) + buf[4*(        j)];
338         buf[4 * ( 9 + 8 - j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
339         buf[4 * (         j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2         + j], 1);
340         i += 4;
341     }
342
343     s0 = tmp[16];
344     s1 = MULH3(tmp[17], icos36h[4], 2);
345     t0 = s0 + s1;
346     t1 = s0 - s1;
347     out[(9 + 4) * SBLIMIT] = MULH3(t1, win[     9 + 4], 1) + buf[4*(9 + 4)];
348     out[(8 - 4) * SBLIMIT] = MULH3(t1, win[     8 - 4], 1) + buf[4*(8 - 4)];
349     buf[4 * ( 9 + 4     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
350     buf[4 * ( 8 - 4     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
351 }
352
353 void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
354                                int count, int switch_point, int block_type)
355 {
356     int j;
357     for (j=0 ; j < count; j++) {
358         /* apply window & overlap with previous buffer */
359
360         /* select window */
361         int win_idx = (switch_point && j < 2) ? 0 : block_type;
362         INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
363
364         imdct36(out, buf, in, win);
365
366         in  += 18;
367         buf += ((j&3) != 3 ? 1 : (72-3));
368         out++;
369     }
370 }
371